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.

BoxedUnit cannot be cast to class scala.Function0

See original GitHub issue

Compiler version

Minimized code

Welcome to Scala 3.1.3-RC1-bin-SNAPSHOT-git-d38e10e (17.0.2, Java OpenJDK 64-Bit Server VM).
Type in expressions for evaluation. Or try :help.

scala> def g(args: Int*)(block: => Unit): Unit = println(s"Well, ${args.sum} and $block")
def g(args: Int*)(block: => Unit): Unit

scala> def f = g()
def f: (=> Unit) => Unit

scala> f(println("x"))
x
java.lang.ClassCastException: class scala.runtime.BoxedUnit cannot be cast to class scala.Function0 (scala.runtime.BoxedUnit and scala.Function0 are in unnamed module of loader java.net.URLClassLoader @535b8c24)
  at scala.runtime.function.JProcedure1.apply(JProcedure1.java:15)
  at scala.runtime.function.JProcedure1.apply(JProcedure1.java:10)
  ... 33 elided

Expectation

Welcome to Scala 2.13.8 (OpenJDK 64-Bit Server VM, Java 17.0.2).
Type in expressions for evaluation. Or try :help.

scala> def g(args: Int*)(block: => Unit): Unit = println(s"Well, ${args.sum} and $block")
def g(args: Int*)(block: => Unit): Unit

scala> def f = g()
                ^
       error: missing argument list for method g
       Unapplied methods are only converted to functions when a function type is expected.
       You can make this conversion explicit by writing `g _` or `g(_)(_)` instead of `g`.

scala> def f = g() _
def f: (=> Unit) => Unit

scala> f(println("x"))
x
Well, 0 and ()

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
som-snyttcommented, Mar 19, 2022

I confirmed it also compiles under separate compilation, so I’ll try to see what’s wrong with REPL.

1reaction
oderskycommented, Mar 19, 2022

It only happens in the repl of main as far as I can see. The following program works as expected:

object l1:
  def g(args: Int*)(block: => Unit): Unit = println(s"Well, ${args.sum} and $block")
object l2:
  import l1.*
  def f = g()
@main def Test =
  import l1.*
  import l2.*
  f(println("x"))
Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot be cast to class scala.runtime.BoxedUnit - Community
Hi! I encountered an unexpected behavior at runtime. Consider the following code: def execute[R](): R = (new Object).asInstanceOf[R] val r1: Unit ...
Read more >
Cast BoxedUnit to scala tuple - apache spark - Stack Overflow
Hello everybody i have an error during running my spark scala code . it says java.lang. ... BoxedUnit cannot be cast to scala.Tuple4....
Read more >
scala.runtime.BoxedUnit Java Examples - ProgramCreek.com
This page shows Java code examples of scala.runtime.BoxedUnit.
Read more >
Scala callbacks in Kotlin - Alexey Soshin - Medium
data class KotlinClosable ( ... Scala's Function0 is not Kotlin's Function0 , nor Scala's Unit is Kotlin's Unit ... BoxedUnit cannot be cast...
Read more >
Java Examples for scala.runtime.BoxedUnit - Javatips.net
This java examples will help you to understand the usage of scala.runtime.BoxedUnit. These source code samples ... BoxedUnit.class); kryo.register(scala.
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