Discussion Thread: DIP 1034--Add a Bottom Type (reboot)--Community Review Round 1

wolframw wolframw at protonmail.com
Thu May 7 14:49:39 UTC 2020


On Thursday, 7 May 2020 at 13:45:41 UTC, Dennis wrote:
> Do you have a particular language you'd like to see compared?

Even though it's not a systems programming language, Scala could 
be a good example. Scala has a notoriously strict and 
sophisticated type system that is very much inspired by type 
theory: There is a top type called "Any" (which is a supertype of 
every other type), a bottom type called "Nothing" (which is a 
subtype of every other type), and void type is referred to as 
"Unit". (see [1] for a diagram of Scala's type system)
In Scala, "Nothing" is a non-instantiable type that is used for 
anything that doesn't return. Conceptually it appears to be very 
similar to what is envisioned in the DIP.
Here are a few "Nothing" examples:

val foo = throw new Exception();  // compiles; `foo` will have 
type `Nothing`
                                   // `foo` will never be assigned 
to

def bar(n: Int): String = {
     n match {
         case 1 => "one"
         case 2 => "two"
         // note that even though the return type is `String`, the 
following is
         // valid because `Nothing` is a subtype of `String` (and 
every other type)
         case _ => throw new Exception()  // `case _` is 
equivalent to `default` in D
         // alternatively, `assert(false)` would give a similar 
result
         // `case _ => Nothing` would NOT work because `Nothing` 
is non-instantiable
     }
}

val baz = () => throw new Exception()
// type of baz is Function0[Nothing]
// meaning: a nullary function that returns `Nothing` (i.e. 
doesn't return at all)

As one would expect, the Scala standard library uses "Nothing" as 
the return type of its exit and error functions: see [2] (notice 
the hack in exit: Since Java's System.exit returns void -- or 
"Unit" in Scala lingo -- the exit function throws, just so its 
return type can be "Nothing")

[1] 
https://docs.scala-lang.org/resources/images/tour/unified-types-diagram.svg
[2] 
https://github.com/scala/scala/blob/2.13.x/src/library/scala/sys/package.scala#L22



More information about the Digitalmars-d mailing list