I'm trying to get a list of classes of other classes and structs. I currently have the following code (reduced to minimum)
struct Foo
end
class Bar
end
alias Baz = Foo.class | Bar.class
types = [
Foo, Bar, Foo, Foo, Bar, Baz
] of Baz
This gives me the following error:
Error in ./test.cr:9: instantiating 'Array(Bar:Class | Foo:Class):Class#build(Int32)'
types = [
^
in ./test.cr:9: instantiating 'Array(Bar:Class | Foo:Class):Class#build(Int32)'
types = [
^
in ./test.cr:9: no overload matches 'Pointer(Bar:Class | Foo:Class)#[]=' with types Int32, (Bar:Class | Foo:Class):Class
Overloads are:
- Pointer(T)#[]=(offset, value : T)
types = [
^
I need this to perform type-checking on values generated at runtime and of an unknown type. The types inside the array are known at compile-time. The types against which I need to compare them are not. Small example (pseudocode)
types = [TString, TFunc] # struct, class
arg_0 = arguments[i]
arg_type = types[i]
if arg_0.is_a? arg_type
# passed
else
# failed
end
This code is being generated in a macro. Is this possible? Am I missing something?
Thanks in advance
You are putting Baz inside the array, which is neither Foo nor Bar, it's a union type class. If you remove Baz from the array then it works fine.