Skip to content

Instantly share code, notes, and snippets.

@jessitron
Created April 16, 2014 22:59
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 jessitron/10940708 to your computer and use it in GitHub Desktop.
Save jessitron/10940708 to your computer and use it in GitHub Desktop.
scala is so weird
scala> class Banana[P] {}
defined class Banana
// We will know when this gets called, when the banana is needed
scala> implicit def myBanana[P]: Banana[P] = { println("banana time"); new Banana[P] }
myBanana: [P]=> Banana[P]
// method with implicits
scala> def addWithBanana(one: Int)(two: Int)(implicit banana: Banana[Int]) = {println(banana); one + two}
addWithBanana: (one: Int)(two: Int)(implicit banana: Banana[Int])Int
// eta-expanded
scala> val etaAddWithBanana = addWithBanana _
etaAddWithBanana: Int => (Int => Int) = <function1>
scala> etaAddWithBanana(1)(2)
banana time
$line3.$read$$iw$$iw$Banana@3be6534d
res0: Int = 3
scala> // it looks like the implicit is resolved when the eta-expression function is applied. But...
scala> implicit val bint: Banana[Int] = new Banana[Int]
bint: Banana[Int] = Banana@24a7d709
scala> // but no! it's resolved to the def at eta-expansion time, even though the def isn't executed until application.
scala> etaAddWithBanana(1)(2)
banana time
$line3.$read$$iw$$iw$Banana@46ba7d0f
res1: Int = 3
scala> // see, we still constructed a new banana, it didn't use the val.
scala> addWithBanana(1)(2)
$line3.$read$$iw$$iw$Banana@24a7d709
res2: Int = 3
scala> // whereas, calling the original method resolves to the implicit val, which has priority over the def.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment