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 flatmap invocation explicit
  • sa covariant (wtf?)
  • some other type nothing used in p2 (e.g. null)
  • sb not occur in return type of flatmap or occurs in covariant position of return type (e.g. return type option[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

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

python - build a suggestions list using fuzzywuzzy -