2

In scala Nothing is a subtype of every other type.

scala> class A {}
defined class A

scala> def x[T >: Nothing](t: T): Unit = {}
x: [T](t: T)Unit

scala> x(new A)

When we create an arbitrary class, it automatically becomes a supertype of Nothing

  1. How this property is maintained in scala? Does the compiler makes Nothing extend every other class at compile time?
  2. Like this way, is it possible to define a custom class X as a subtype of a set of classes(say set s) without making X extend from all the classes in the s? (e.g: Class X is a subtype of all the classes in package com.myproject.models)

Please share your thoughts.

OlivierBlanvillain
  • 7,701
  • 4
  • 32
  • 51
tharindu_DG
  • 8,900
  • 6
  • 52
  • 64

1 Answers1

6

How this property is maintained in scala? Does the compiler makes Nothing extend every other class at compile time?

Nothing is not implemented with an actual class, it's a pure artifact of the compiler. The actual handling of the Nothing type can be vulgarized as hardcoding Nothing in several places inside the compiler, for instance, isSubType(t1: Type, t2: Type): Boolean is unconditionally true when t1 == Nothing.

Like this way, is it possible to define a custom class X as a subtype of a set of classes(say set s) without making X extend from all the classes in the s?

No.

OlivierBlanvillain
  • 7,701
  • 4
  • 32
  • 51