question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Code not compiling when Unit-returning SAM and overloaded methods involved

See original GitHub issue

I 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:closed
  • Created 2 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
smartercommented, Sep 17, 2021

Thanks, this is great!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found