scala - Bizzare type inference limitation - multiple type params -
why not compile?
trait lol[a, sa] { def flatmap[b, sb](f: => lol[b, sb]): lol[b, sb] = ??? } val p1: lol[int, string] = ??? val p2: lol[double, nothing] = ??? val p5 = p1.flatmap(_ => p2) result:
found : int => lol[double,nothing] required: int => lol[double,sb] val p5 = p1.flatmap(_ => p2) ^ things start compile when either:
- type params of
flatmapinvocation explicit sacovariant (wtf?)- some other type
nothingused inp2(e.g.null) sbnot occur in return type offlatmapor occurs in covariant position of return type (e.g. return typeoption[sb])
the above workarounds not acceptable me.
@retronym's commentary on si-9453 explains behaviour you're seeing. here's workaround of sorts ...
we can synthesize type equivalent nothing won't cause typer retract inference solution,
type reallynothing = nothing { type t = unit } ie. nothing dummy refinement. example of question,
scala> :paste // entering paste mode (ctrl-d finish) trait lol[a, sa] { def flatmap[b, sb](f: => lol[b, sb]): lol[b, sb] = ??? } val p1: lol[int, string] = ??? val p2: lol[double, reallynothing] = ??? val p5 = p1.flatmap(_ => p2) // exiting paste mode, interpreting. scala.notimplementederror: implementation missing @ scala.predef$.$qmark$qmark$qmark(predef.scala:225) ... 37 elided
Comments
Post a Comment