I though that in the inherited
macro @type
would be the inheriting type (aka the subclass), so I believed that I can write macros that expect a particulart constant (or variable) to be declared in a subclass.
But this example proves me wrong.
EDIT:
executing this code
module Foo
class Bar
macro inherited
{{ p! @type.has_constant?("C1")}}
{{ p! @type.class_vars}}
{{ p! @type}}
def value
@value.value
end
def initialize(@value : Int32)
end
def |(other : {{@type}})
{{@type}}.new(@value | other.value)
end
def &(other : {{@type}})
{{@type}}.new(@value & other.value)
end
end
end
end
class MyBar < Foo::Bar
C1 = "SomeString"
{{ p! @type.has_constant?("C1") }}
end
puts
The output is:
@type.has_constant?("C1") # => false
@type.class_vars # => []
@type # => MyBar
@type.has_constant?("C1") # => true
Can someone explain this to me?