Created
April 16, 2014 22:59
-
-
Save jessitron/10940708 to your computer and use it in GitHub Desktop.
scala is so weird
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
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