Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't Unit extend Product in Scala?

In Scala, why doesn't Unit extend the Product trait just like the Tuple* classes and case classes (including the "empty" ones, like in case class Empty(), and case objects)?

Unit (the unit value () to be more specific) definitely stands as the empty product and tuple. It is used this way in shapeless, for example.

like image 655
Alex Archambault Avatar asked Aug 01 '14 20:08

Alex Archambault


1 Answers

Unit is not analogous to to an empty collection, so it doesn't make sense to make it a product of arity zero. Even though a Product0 wouldn't contain any data, it would still define behaviour via the productElement and productIterator methods.

The goal of Unit is to represent the absence of any behaviour or data beyond the bare minimum defined in Object. In fact, the getClass() function returns null to indicate that it has no type, no by definition it should have no metadata associated with it.

If you were to make Unit extend Product, then it would violate the concept of "no type".

like image 168
Tompey Avatar answered Oct 20 '22 00:10

Tompey