Created
June 17, 2013 15:38
-
-
Save samueltardieu/5797887 to your computer and use it in GitHub Desktop.
Scala seems to always prefer value over call by name: `foo(true)` returns "foo1" and is not ambiguous. Why?
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
object Test { | |
def foo(t: Boolean) = "foo1" | |
def foo(t: => Boolean) = "foo2" | |
} |
and foo ( (() => foo (true) == "foo1") )
returns actually "foo2" ?…
(Scala RM 4.6.1)
@bateast: this doesn't compile for me (2.10.2), none of the two methods is applicable to an argument of type () => Boolean
, because => T
isn't a type, and isn't considered like a Function0[T]
for applicability but like a T
, just a weird, different one...
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ah, the joy of overloading resolution (SLS 6.26.3):
foo2
can always be called withfoo1
's parameter, but not the other way around (it would require evaluating the by-name parameter), sofoo1
is more specific and no ambiguity error is returned.