You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Snippet below fails with ClassCastException at runtime.
import language.higherKinds
object FooExample extends App {
// Slice of comonad is where this came up
trait Foo[F[_]] {
def coflatMap[A, B](f: F[A] => B): F[A] => F[B]
}
// A non-empty list
case class Nel[A](head: A, tail: List[A])
object NelFoo extends Foo[Nel] {
// It appears that the return type for recursive calls is not inferred
// properly, yet no warning is issued. Providing a return type or
// type arguments for the recursive call fixes the problem.
def coflatMap[A, B](f: Nel[A] => B) = // ok w/ return type
l => Nel(f(l), l.tail match {
case Nil => Nil
case h :: t => {
val r = coflatMap(f)(Nel(h, t)) // ok w/ type args
r.head :: r.tail
}
})
}
// Without a recursive call all is well, but with recursion we get a
// ClassCastException from Integer to Nothing
NelFoo.coflatMap[Int, Int](_.head + 1)(Nel(1, Nil)) // Ok
NelFoo.coflatMap[Int, Int](_.head + 1)(Nel(1, List(2))) // CCE
}
The text was updated successfully, but these errors were encountered:
b74c33e represents the exact moment of progression. We should
investigate that a little further so we can fully connect the dots.
But that diff is a bit troublesome: what it the TypeVar ?A1 doing in the post-typer tree? I will pull on that thread to see what I can find.
I chatted to Lukas today about his patch to Namers that (ostensibly) fixed this, and it seems to me that our current scheme for allowing recursive methods to use inherited inferred return types is quite risky.
scala>traitA { deffoo:Any }; objectBextendsA { deffoo= {if(false) foo; "" } }; B.foo
defined traitA
defined module B
res0:String=""
During typechecking of B#foo, the method has the return type Any, which is later refined to String.
We can see this by trying to ascribe the more specific type:
Snippet below fails with ClassCastException at runtime.
The text was updated successfully, but these errors were encountered: