-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Parameterless function identified as function with one Unit parameter #10437
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This is different behaviour to Scala 2. In Scala 2 the implicit conversion to |
The first two overloads are applicable, but the third is chosen in Scala 3. There is a different error reported if renamed to avoid the overload:
The first workaround makes it look like a syntactic parsing error, but the second workaround looks like a typing error during overload resolution. |
For anyone coming here, with Scala 3 RC1 there is no more need for the implicit conversion to final class MyClass(name: String) {
def apply(fun: => Any): Unit = {
println("Got a By-name parameter")
}
@targetName("apply_fun0")
def apply(fun: () => Any): Unit = {
println("Got a Function0")
}
def apply[T1](f: T1 => Any): Unit = {
println("Got a Function1")
}
} For instance, following code MyClass("abc") {
1
}
MyClass("abc") { () =>
1
}
MyClass("abc") { (s: String) =>
1
} produces following output:
|
Closes scala#9225 Closes scala#10437
Minimized code
Output
From what I understand the
() => f
is interpreted asUnit => Any
rather than() => Any
.Expectation
This code compiles fine with Scala 2.13. Although I admit it's a bit twisted, I expected it to compile in Scala 3 as well.
Note that I found two workarounds:
This might be intended behavior and I can accept it easily but I prefer to post and ask in case this could be the indicator of a more serious issue.
The text was updated successfully, but these errors were encountered: