10

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.

8
  • Can the down voter explain himself ? Commented Aug 1, 2014 at 21:02
  • You could read existing question stackoverflow.com/questions/16173477/… before asking again . Commented Aug 1, 2014 at 22:46
  • 6
    @applicius - That question asks a different question and the answer for this question is not found in the answers for that question. Can't you tell the difference? Commented Aug 1, 2014 at 23:41
  • @applicius I looked at the question you linked to, and agree with Rex Kerr, I'm just not asking the same question (and the answer to mine is not in your link). Commented Aug 2, 2014 at 1:55
  • 2
    en.wikipedia.org/wiki/Tuple. The Unit should be Tuple0, maybe its just a mistake of scala. Commented Aug 2, 2014 at 23:24

2 Answers 2

1

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".

Sign up to request clarification or add additional context in comments.

3 Comments

I don't know which version of scala you're using, but ().getClass() does not return null in 2.11.2. () does have a type - Unit e.g. val c: Class[Unit] = ().getClass().
Yes it does. From the 2.11.2 sources: final abstract class Unit private extends AnyVal { override def getClass(): Class[Unit] = null }. Or in a worksheet: ().getClass; res0: Class[Unit] = void
Despite that, ().getClass() == null and ().getClass eq null are both false, so that void you're seeing is different from null. As the comment above class Unit says, () "is not represented by any object in the underlying runtime system." It's treated specially.
0

The current Scala Language Specification seems to requires this in conforming implementations of Scala, according to section 12.2.3:

Class Unit ... implements only the three methods equals, hashCode, and toString from class Any.

To extend Product and implement its methods, this part of the specification would have to change.

1 Comment

Of course, they could just change the specification, right? Can this question can be answered any better than "because they didn't do that"?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.