Last active
September 4, 2015 16:22
-
-
Save gabro/a98246e59585cc425160 to your computer and use it in GitHub Desktop.
Subtle scala quirk with implicit conversions
This file contains hidden or 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
@ "42".toInt | |
res0: Int = 42 | |
@ implicit object Foo extends Function1[String, Int] { | |
def apply(s: String) = s.toInt | |
} | |
defined object Foo | |
@ "42”.toInt | |
// BOOM (infinite recursion) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Explanation: calling
toInt
(not defined onString
) triggers an implicit lookup, which usually hits thetoInt
method defined onStringLike
Too bad that
toInt
is defined also onInt
and we're providing an implicit function fromString
toInt
, which has the precedence (it's "closer" in the scope) and usestoInt
.Result (after implicit expansion):