Given this POC:
class Test
getter test : String -> String?
def initialize(&handler : String -> _)
@test = ->(input : String) do
res = handler.call("banana")
res.is_a?(String) ? res : nil
end
end
end
then p Test.new { "123" }.test.call("test")
works as expected, but p Test.new { 123 }.test.call("test")
throws Error: instance variable '@test' of Test must be Proc(String, (String | Nil)), not Proc(String, Nil)
Apparently the compiler is smart enough to figure out in the latter case that the proc can never return anything but nil
. But I would assume it’s an acceptable sub-set.
What am I failing to grasp here?