Created
October 3, 2012 22:13
-
-
Save Blaisorblade/3830228 to your computer and use it in GitHub Desktop.
Bugreport example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object Foo { | |
trait Exp[+T] | |
case class Const[+T](t: T) extends Exp[T] | |
implicit def fooPure[T](t: T): Exp[T] = Const(t) | |
class WithAsFoo[T](t: T) { | |
def asFoo(implicit conv: T => Exp[T]) = conv(t) | |
} | |
implicit def toWithAsFoo[T](t: T) = new WithAsFoo(t) | |
implicit def toMapSeq[T](e: Exp[Seq[T]]) = new MapSeq(e) | |
class MapSeq[T](e: Exp[Seq[T]]) { | |
def map[U](f: Exp[T] => Exp[U]): Exp[Seq[U]] = throw new Exception() | |
def head: Exp[T] = throw new Exception() | |
} | |
def assertType[T](t: T) {} | |
//val empty = Seq.empty.asFoo | |
val empty = Const(Seq.empty) | |
val v = empty.map(x => empty.head) | |
//Does not work: | |
assertType[Exp[Seq[Nothing]]](v) | |
/* | |
Error: | |
Foo.scala:21: error: type mismatch; | |
found : Foo.Exp[Seq[T]] | |
required: Foo.Exp[Seq[Nothing]] | |
assertType[Exp[Seq[Nothing]]](v) | |
^ | |
*/ | |
//Now comment the first error out and get another funny error (which comes at a later stage). | |
//This works | |
empty.map(x => empty.head) | |
//The following | |
//Does not work: | |
import scala.reflect.runtime.universe.TypeTag | |
def f[T: TypeTag](e: Exp[T]): T = throw new Exception() | |
f(empty.map(x => empty.head)) | |
/* | |
Error: | |
Foo.scala:38: error: type parameter not specified | |
f(empty.map(x => empty.head)) | |
^ | |
one error found | |
With 2.10.0-M7 the message was even funnier: | |
Foo.scala:38: error: macro has not been expanded | |
*/ | |
} | |
//This code only works in 2.10. | |
object TestNoExp { | |
//Let's try without Exp | |
import scala.reflect.runtime.universe.TypeTag | |
def f[T: TypeTag](e: T): T = throw new Exception() | |
val empty3 = Seq.empty | |
//Works: | |
empty3.map(x => empty3.head) | |
//Also fine | |
f(empty3.map(x => empty3.head)) | |
val empty4 = Seq.empty[Int] | |
//Works: | |
empty4.map(x => empty4.head) | |
//Also fine | |
f(empty4.map(x => empty4.head)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment