Skip to content

Instantly share code, notes, and snippets.

@Blaisorblade
Created March 21, 2012 10:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Blaisorblade/2146097 to your computer and use it in GitHub Desktop.
Save Blaisorblade/2146097 to your computer and use it in GitHub Desktop.
Showcase bug SI-3346 and applications to deep embeddings
//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
*/
//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!
//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