To be perfectly honest, I have no idea if Unit
object can be removed! If that’s possible to do, that’d be great. However, looking only as far as AnyValCompanion
, we have:
package scala
/** A common supertype for companion classes of primitive types.
*
* A common trait for /companion/ objects of primitive types comes handy
* when parameterizing code on types. For instance, the specialized
* annotation is passed a sequence of types on which to specialize:
* {{{
* class Tuple1[@specialized(Unit, Int, Double) T]
* }}}
*
*/
private[scala] trait AnyValCompanion extends Specializable { }
Which means that Unit
companion object is used as an argument to specialized
annotation. This’d need to be adjusted if we rewired Unit
to mean ()
.
In addition, I have no idea if the object isn’t accessed directly by the compiler to box/unbox ()
, or even if the notion of boxing/unboxing makes sense for Unit
. Also, the fact that Unit
will then be the sole exception to the rule “all primitive types have a companion object” is not entirely positive. Removing Unit
object really does not seem like a small change to me, and I think we agree that the problem isn’t too big.
I’ve recounted, and out of 6 people at my workplace that tried to use ()
(myself included), 4 used Unit
in some form instead (including one case where we ended up with Unit.type
instead of Unit
!). We may be an outlier, but I doubt it seeing the reactions of people to this kind of code compiling. The warning on Unit
object discard would help, would take about ~10 LOC judging from the implementation of -Ywarn-value-discard
, and I’m perfectly willing to do it. It could even continue to be useful after further changes to value discard: consider that if it was restricted to methods, functions and by-name parameters as I proposed, Unit : Unit
would no longer compile, and following code would trigger a warning:
scala> (() => Unit) : (() => Unit)
<console>:11: warning: discarded Unit object of type Unit.type. To create a value of type Unit, use ().
(() => Unit) : (() => Unit)
^
The problem isn’t too big, so the solution shouldn’t be too complex either. This’d be one puzzler less for the newcomers.