I have a class hierarchy and I want to be able to call a specific ancestor’s implementation of a method as I traverse the hierarchy. I am using super calls to move up the hierarchy, but then I want to be able to call the current @type’s implementation of another virtual method.
Here’s my example
class MachineState
def on_entry
# empty
end
def on_entry_recurse(old_state : MachineState)
# empty
end
macro inherited
# Entry starts below the top common state and progresses down
def on_entry_recurse(old_state : MachineState)
# stop once we reach a common ancestor
if old_state.class > {{@type}}
super
on_entry # calls virtual method - not what we want
# maybe something like: {{@type}}::on_entry ??
end
end
end
end
class S1 < MachineState
def on_entry
puts "In S1"
end
end
class S11 < S1
def on_entry
puts "In S11"
end
end
class S111 < S11
def on_entry
puts "In S111"
end
end
describe "test it" do
it "rescurses through ancestors" do
s1 = S1.new
s111 = S111.new
s111.on_entry_recurse(s1)
# WANTED: In S11\n In S111
# ACTUAL: In S111\nIn S111
end
end