Search code examples
typescrystal-lang

Crystal lang : Type arithmetic, belongs to a Union?


I'm trying to implement a fucntion:

def foo(t : Class)
    if t in Int::Signed
        # ...
    end
end

But how to implement t in Int::Signed? Where Int::Signed

I know is_a?(Int::Signed) but here the parameter is of type Type. Thanks.


Solution

  • def foo(t : Class)
      if t < Int::Signed
        # ...
      end
    end
    

    Class#< is only added in Crystal 0.25, if I am not mistaken, so make sure you update if it does not work for you. There is also Class#<= that would return true for Int::Signed <= Int::Signed.