Crystal: Instance variable '@callback' of Foo must be (Proc((T | Nil)) | Nil), not Proc(Nil)

Created on 10 Aug 2016  路  2Comments  路  Source: crystal-lang/crystal

Hi, I cannot wrap my head around below piece of code... What am I doing wrong?
https://carc.in/#/r/16r0

bug compiler

All 2 comments

reduced

class Foo
  @callback : Proc(Bool?)? = -> {}
end

Error in line 2: instance variable '@callback' of Foo must be (Proc((Bool | Nil)) | Nil), not Proc(Nil)

Right now, a proc that returns X isn't implicitly castable to a proc that returns X | Y.

In your case you have ->{}, which is Proc(Nil), but you require the callback to be Proc(Bool | Nil). It's solved if you do Proc(Bool?).new { } instead of ->{}.

I'm still not sure the rules should change. This works but maybe shouldn't:

def foo(block : -> Int32 | String)
end

foo ->{ 1 }
Was this page helpful?
0 / 5 - 0 ratings