Trying to understand the order methods will be recognized in the case of overloading.
I have this test case:
class Foo
def bar(a, b, c)
"bar1"
end
def bar(a, b, c = 1)
"bar2"
end
def bar(a, b = 1, c = 2)
"bar3"
end
def bar(a = 1, b = 2, c = 3)
"bar4"
end
def boo(a) # restriction 4 (least restrictive, but its never used actually)
"boo1"
end
def boo(a : String) # restriction 1 (most restrictive)
"boo2"
end
def boo(a : String | Int32) # restriction 2
"boo3"
end
def boo(a : String | Int32 | Bool) # restriction 3 <- this will be defined last, and so will get used in all cases?
"boo4"
end
end
puts Foo.new.bar(1, 2, 3)
puts Foo.new.bar
puts Foo.new.boo(12)
puts Foo.new.boo(true)
which produces:
bar4
bar4
boo3
boo4
okay, now if I switch things around, I would still expect the same result.
but instead I get an error (wrong number of params) unless the definition with all three default params of bar is last.
More over, if I switch the two definitions of boo around it prints boo4 twice. I.e. it doesn’t seem that the ordering is actually doing anything.
class Foo
def bar(a, b, c)
"bar1"
end
def bar(a, b, c = 1)
"bar2"
end
def bar(a = 1, b = 2, c = 3)
"bar4"
end
def bar(a, b = 1, c = 2)
"bar3"
end
def boo(a) # restriction 4 (least restrictive, but its never used actually)
"boo1"
end
def boo(a : String) # restriction 1 (most restrictive)
"boo2"
end
def boo(a : String | Int32 | Bool) # restriction 3 <- this will be defined last, and so will get used in all cases?
"boo4"
end
def boo(a : String | Int32) # restriction 2
"boo3"
end
end
puts Foo.new.bar(1, 2, 3)
# puts Foo.new.bar <- this no won't work
puts Foo.new.boo(12)
puts Foo.new.boo(true)
produces
bar3
boo4
boo4