I want to work with some instances of a class Hero that is stored in an array:
class Hero
def test
puts the_type
end
macro the_type
{{@type.id.stringify}}
end
end
class Super < Hero
def test
puts "super test"
end
end
me = [] of Hero
me << Hero.new
me.each(&.test)
outputs
Hero+
However I want to reference the Hero class TypeNode as the Hero+ TypeNode gives me an error when I try to extract a constant from it.
BUG: Hero+ has no types
My actual code is slightly more complex but really just wondering if there is a way to effectively resolve down to the nearest class from a Class+ TypeNode
Or is it possible to ensure that the array is strictly only storing Hero types and not any superclass types?
class Hero
def test
puts {{@type.id.stringify}}
end
end
class Super < Hero
end
me = [] of Hero
me << Hero.new
me << Super.new
me.each(&.test)
outputs
Hero+
Super
Which I feel is unexpected as I understand that the contents of the array should be Hero+ however I’d expect the type inside the Hero class itself to be Hero
Which causes something like this to fail - however this works if you don’t use an array anywhere
class Hero
CONST = "1234"
def test
puts {{([@type] + @type.ancestors.select { |c| c.has_constant?("CONST") }).map { |c| c.constant("CONST") } }}
end
end
class Super < Hero
CONST = "5678"
end
me = [] of Hero
me << Hero.new
me << Super.new
me.each(&.test)
I want to be able to collect the values of constants in an inheritance hierarchy - it’s not even really that much of an issue in practice, I just had a spec that was using an array and it didn’t compile due to the type being Hero+ and it seems like an issue.
I feel like these two bits of code should be equivalent