Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BoxedUnit vs. Unit in Scala

Tags:

scala

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

like image 366
user5279007 Avatar asked Aug 29 '15 18:08

user5279007


1 Answers

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.

like image 108
sjrd Avatar answered Sep 19 '22 01:09

sjrd