Created
March 21, 2012 10:42
-
-
Save Blaisorblade/2146097 to your computer and use it in GitHub Desktop.
Showcase bug SI-3346 and applications to deep embeddings
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
//Paste this code on a fresh Scala console. | |
trait Exp[+T] | |
case class Const[T](t: T) extends Exp[T] | |
implicit def toExp[T](t: T): Exp[T] = Const(t) | |
case class Plus(x: Exp[Int], y: Exp[Int]) extends Exp[Int] | |
class IntOps(x: Exp[Int]) { | |
def +(y: Exp[Int]) = Plus(x, y) | |
} | |
val x = toExp(1) | |
/* | |
* Then paste either one of the below two files. Pasting both together will make the defined | |
* conversions ambiguous | |
*/ |
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
//Alternative 1, does not work: https://issues.scala-lang.org/browse/SI-3346 | |
implicit def intPimp[T](x: T)(implicit conv: T => Exp[Int]) = new IntOps(x) | |
1 + x | |
intPimp(x) + 1 //This line works. | |
x + 1 //Error! |
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
//Workaround | |
implicit def expIntPimp(x: Exp[Int]) = new IntOps(x) | |
implicit def intPimp(x: Int) = expIntPimp(x) | |
1 + x | |
x + 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment