What's the difference between BoxedUnit and Unit in Scala? When are they used as return type of a function? And what's their relationship with REF? Here's an error of type mismatch that I encountered when dealing with Unit.
error: java.lang.AssertionError: assertion failed: Can't convert from UNIT to REF(class BoxedUnit) in unit hello.scala at source-/Users/shiyu/Scala/FinalDataFlow/src/print/hello.scala,line-347,offset=13999
Unit
is the type of the unique value ()
, pronounced "unit".
BoxedUnit
is an implementation detail of Scala on the JVM, used to encode ()
when it enters in a generic context, or is otherwise assigned to Any
. Normally, you shouldn't hear about BoxedUnit
in the first place, although it does leak to some user-level features. For example ((): Any).getClass().getName() == "scala.runtime.BoxedUnit"
.
That said, the error you get is clearly a compiler crash, as evidenced by the AssertionError
. It's not a problem in your code. You should probably report this as a bug if it is not already in the bug database.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With