Skip to content

Instantly share code, notes, and snippets.

@samueltardieu
Created June 17, 2013 15:38
Show Gist options
  • Save samueltardieu/5797887 to your computer and use it in GitHub Desktop.
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?
object Test {
def foo(t: Boolean) = "foo1"
def foo(t: => Boolean) = "foo2"
}
@gourlaysama
Copy link

Ah, the joy of overloading resolution (SLS 6.26.3):

  • they are both applicable, but when in doubt the compiler looks at the specificity of methods:
  • foo2 can always be called with foo1's parameter, but not the other way around (it would require evaluating the by-name parameter), so foo1 is more specific and no ambiguity error is returned.

@bateast
Copy link

bateast commented Jun 17, 2013

and foo ( (() => foo (true) == "foo1") ) returns actually "foo2" ?…

(Scala RM 4.6.1)

@gourlaysama
Copy link

@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