The Scastie interface gives the following error.
`Unit` companion object is not allowed in source; instead, use `()` for the unit value
Is that a thing now that using Unit
as a value is bad form? Is ()
really better?
The Scastie interface gives the following error.
`Unit` companion object is not allowed in source; instead, use `()` for the unit value
Is that a thing now that using Unit
as a value is bad form? Is ()
really better?
Yes. ()
has always been the idiomatic form of the value (as opposed to the type Unit
), and it sounds like that’s now being enforced.
Rather, using Unit
was never supported, and worked by accident.
You can get the functionality back by using any other object.
I think I first used Unit because it was the notation in the Pierce book, Types and Programming Languages.
It works for the same reason
def foo: Unit = 7
Works: when a value of type Unit
is expected, and the last expression is not of type Unit
, the compiler helpfully inserts ()
so you don’t have to.
Object Unit
is not of type Unit
, but it’s own unrelated type (to wit: Unit.type
), and the compiler helpfully inserts ()
.
Using Unit
where you mean ()
can actually trip up inference (e.g. in List(Unit, ())
so it’s now a compiler warning.