5

I see from Scala hierarchy that AnyVal is super type for scala.Unit, Boolean, Char and other Number types.

scala> val list1 = List((),  1 ) 
list: List[AnyVal] = List((), 1)  // I see this is valid when compared with hierarchy tree.

scala> val list2 = List(Unit,  1 )
list: List[Any] = List(object scala.Unit, 1) // Why???

I see list1 is of type AnyVal where as list2 is of type Any, even though they have same data (I assume).

Is () not same as Scala.Unit? What am I missing here?

Puneeth Reddy V
  • 1,538
  • 13
  • 28

2 Answers2

5

To answer your question, () is a value of type scala.Unit. Whereas, scala.Unit is the companion object, so it is of type Unit.type.

Have a look in the REPL code below:

scala> (): scala.Unit
// (): scala.Unit

scala> scala.Unit
// res1: Unit.type = object scala.Unit

Bottom line is any object you pass to a covariant list will find the type common to the values. See discussion in Why doesn't the example compile, aka how does (co-, contra-, and in-) variance work?

As you discovered, the common type of Integer and scala.Unit is AnyVal. The common type of Intger and Unit.type is Any.

ashawley
  • 4,195
  • 1
  • 27
  • 40
Amit Prasad
  • 725
  • 4
  • 17
4

There are 3 distinct entities:

1) type scala.Unit

2) object () - the only member of the class scala.Unit

3) object scala.Unit - a companion object of 1). It is a member of class scala.Unit$ - not the same as scala.Unit.

In your first example () stands for 1), in the second Unit stands for 3)

simpadjo
  • 3,947
  • 1
  • 13
  • 38
  • The type of the companion object is more correctly given as `Unit.type`, as it is the singleton type associated with the value `Unit`. The fact that the class is called `Unit$` is more of an implementation detail. – HTNW Nov 24 '18 at 00:07
  • @HTNW yes, you are right. But I think that digging into the difference between classes and types would be confusing in the context of the question. – simpadjo Nov 24 '18 at 08:23