I’m trying to create a macro that takes generic types as arguments but I keep getting Error: undefined constant.
Example code:
macro my_macro(t1, t2)
# code ...
end
class Foo(T)
def bar(x : U) forall U
# code ...
bar_var = my_macro(T, U) # <-- Error: undefined constant U
# more code ...
end
end
I have no idea why I get this error because, if I understand this this correctly, it should be possible?
Here is my actual code - maybe I missed something?
macro generate_vector(t1, t2)
{% if !(t1.resolve < Float) && t2.resolve < Float %}
# code ...
{% else %}
# code ...
{% end %}
end
class Vector(T)
def +(other : Vector(U)) forall U
if self.size != other.size
raise ArgumentError.new("vectors must be same size")
end
vec = generate_vector(T, U)
# Error: undefined constant U :/
self.each_with_index do |elem, i|
vec << elem + other[i]
end
return vec
end
end