Type inference with closures can lead to type parameters being assigned a potentially confusing/wrong value
This snippet:
class Simple[IN, OUT] {
let @block: fn (mut IN) -> Option[OUT]
fn static new(block: fn (mut IN) -> Option[OUT]) -> Self {
Self { @block = block }
}
}
Results in this compile-time error:
/home/yorickpeterse/Downloads/test.inko:122:5 error(invalid-type): Incorrect type: expected 'Simple[IN, OUT]', found 'Simple[mut IN, OUT]'
I think what happens here is the following: when we initialise the Simple
type, IN
and OUT
are not yet assigned. When comparing the closures we end up comparing mut IN
from block
with mut IN
from @block
, observe that IN
is unassigned and assign it to mut IN
from block
.
This is technically correct, but I'm not sure it's the behaviour that I would want. In the above example, the value of IN
can still be anything, it's just that the closure stored in Simple
expects a mutable reference.