Code not compiling when Unit-returning SAM and overloaded methods involved
See original GitHub issueI think there seems to be a problem with SAM conversions and choosing the right overloaded method. Sorry if the bug already exists I couldn’t find one.
Compiler version
Scala 3.0.2 and 3.1.0-RC2
Minimized code
You can find a reproducer here: https://github.com/jlprat/overload-sam-reproducer/
import java.lang.Throwable
import java.util.function.Supplier
object Reproducer {
//assertThrows has 3 overloads, 2 with 3 parameters and 1 with only 2 parameters.
//This overload is taking a subclasss of `Throwable`, and an `Executable` which is a parameterless SAM returning void
assertThrows(classOf[IllegalArgumentException], () => 3)
//This overload is taking a subclasss of `Throwable`, an `Executable` which is a parameterless SAM returning void, and a `String`
assertThrows(classOf[IllegalArgumentException], () => 3, "This is a message")
//This overload is taking a subclasss of `Throwable`, an `Executable` which is a parameterless SAM returning void, and a `Supplier` returning `String`
assertThrows(classOf[IllegalArgumentException], () => 3, () => "This is a message")
def assertThrows[T <: Throwable](clazz: Class[T], executable: Executable): Unit = ???
def assertThrows[T <: Throwable](clazz: Class[T], executable: Executable, message: String): Unit = ???
def assertThrows[T <: Throwable](clazz: Class[T], executable: Executable, supplier: Supplier[String]): Unit = ???
@FunctionalInterface
trait Executable {
@throws[Throwable]
def execute(): Unit
}
}
Output
[warn] -- [E129] Potential Issue Warning: /home/josep.prat/playground/overload-sam-reproducer/src/main/scala/example/Reproducer.scala:11:56
[warn] 11 | assertThrows(classOf[IllegalArgumentException], () => 3)
[warn] | ^
[warn] |A pure expression does nothing in statement position; you may be omitting necessary parentheses
[error] -- [E134] Type Error: /home/josep.prat/playground/overload-sam-reproducer/src/main/scala/example/Reproducer.scala:14:2
[error] 14 | assertThrows(classOf[IllegalArgumentException], () => 3, "This is a message")
[error] | ^^^^^^^^^^^^
[error] |None of the overloaded alternatives of method assertThrows in object Reproducer with types
[error] | [T <: Throwable]
[error] | (clazz: Class[T], executable: example.Reproducer.Executable, supplier:
[error] | java.util.function.Supplier[String]
[error] | ): Unit
[error] | [T <: Throwable]
[error] | (clazz: Class[T], executable: example.Reproducer.Executable, message: String
[error] | ):
[error] | Unit
[error] | [T <: Throwable]
[error] | (clazz: Class[T], executable: example.Reproducer.Executable): Unit
[error] |match arguments ((classOf[IllegalArgumentException] : Class[IllegalArgumentException]), () => Int, ("This is a message" : String))
[error] -- [E134] Type Error: /home/josep.prat/playground/overload-sam-reproducer/src/main/scala/example/Reproducer.scala:17:2
[error] 17 | assertThrows(classOf[IllegalArgumentException], () => 3, () => "This is a message")
[error] | ^^^^^^^^^^^^
[error] |None of the overloaded alternatives of method assertThrows in object Reproducer with types
[error] | [T <: Throwable]
[error] | (clazz: Class[T], executable: example.Reproducer.Executable, supplier:
[error] | java.util.function.Supplier[String]
[error] | ): Unit
[error] | [T <: Throwable]
[error] | (clazz: Class[T], executable: example.Reproducer.Executable, message: String
[error] | ):
[error] | Unit
[error] | [T <: Throwable]
[error] | (clazz: Class[T], executable: example.Reproducer.Executable): Unit
[error] |match arguments ((classOf[IllegalArgumentException] : Class[IllegalArgumentException]), () => Int, () => String)
Expectation
Code should compile
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (3 by maintainers)
Top Results From Across the Web
Code not compiling when Unit-returning SAM and overloaded ...
My view on this is that I think the auto-insertion of () makes sense when a Java method is called but not a...
Read more >Why Compiler Confused in compiling Java code [duplicate]
In Integer and String overload method,Compiler could not able to decide which one to call.Why this happening?
Read more >programming kotlin - Sistemas da Informação - 35 - Passei Direto
The compiled code would be equivalent to the following snippet: ... an overload and it is ambiguous which SAM type to promote the...
Read more >the code below works perfectly for me in Xcode 9.2 but keep ...
Overriding Methods challenge - the code below works perfectly for me in Xcode 9.2 but keep getting 'code won't compile'.
Read more >What's new in Kotlin 1.4
Before Kotlin 1.4.0, you could apply SAM (Single Abstract Method) ... to the function foo takes no arguments, the default value 0 is...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Thanks, this is great!
Thread created under https://contributors.scala-lang.org/t/code-not-compiling-when-unit-returning-sam-and-overloaded-methods-involved/5271