The Yin and Yang of Scala: Any and Nothing

Ocean Blue
Oct 27, 2020

--

There are Yin and Yang in Scala.

The Yin:

  • Nothing: The sub type of all types, but have no instance.
  • Null: The sub type of all AnyRef type, with the only instance as null.
  • None: The sub type of Option. The singleton represents non-existent values.
  • Nil: The empty list object. Equivalent to List[Nothing].
  • Unit: The type of a function when it doesn’t return anything. Equivalent to void.

The Yang:

  • Any: The super type of all types.
  • AnyRef : A sub type of Any; and super type of all objects such as String, Map and user-defined types.
  • AnyValue: The other sub type of Any; and the super type of all value types such as Int, Double and so on.

--

--

Ocean Blue
Ocean Blue

No responses yet