How to make union types macro work also on base classes?

I need a method that gets one argument, potentially of union type, that should distinguish with the help of a macro to allow for an extra block per possible single type.
The problem is that the code below doesn’t work anymore with inheritance, since it generates types in the code like Base(Int32|String)+, i.e. with a + at the end - and the compiler chokes.

There’s probably an easy way to fix this, right? :slight_smile:
Thanks a lot - again!

class Base(T)
end

class Child(T) < Base(T)
end

def m(a : T) forall T
    {% for name in T.union_types %}
        a.is_a?({{name.id}}) # gets Error: a.is_a?(Base(Int32 | String)+)
    {% end %}
end

a = [Base(Int32|String).new]
m(a[0])

You could use {{name}} directly, no need to call ASTNode#id. This is definitely a bug though since the + should never be exposed to any Crystal code.

1 Like