Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> Note that void can never be part of a union type, since it indicates "no return value at all".

The empty set is the identity element for set union! It seems very weird to exclude it.



But "void" is not an empty set, it is no set at all...


if we want to squint and talk about sets and functions from sets to other sets...

  function voidy() : void {
    return;
  }
  
  echo json_encode(voidy()); // -> "null"
so i'd say that for most purposes

  void ≡ {null}
a singleton set, more or less[1], sometimes called a "unit type". that's because a void function does return something, it's just entirely uninteresting :) that's different from an "empty" type

  impossible ≡ {} // or ∅
with no values. there are no values of type `impossible`, so `foo() : impossible` can never return anything at all, like this:

  function foo() : impossible {
    while(1){}
  }
see mypy's `NoReturn`, Rust's `!`, Haskell's `Void`. it's used for giving a type to functions like `exit()`

[1] kind of, because it won't let you `return null` explicitly.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: