LEVIATHAN vd52e79c4 · d52e79c486

Types

Merged types

The one rule that types a conditional, a match, an array literal, ?? and a lambda's returns when no type is written: the target first, else the most specific common supertype, else an error.

since 0.1.0-alpha.1linuxwindowswasm

Description

Six places combine several values into one: a conditional c ? a : b, a match expression, an array literal [a, b], a ?? b, the returns of a lambda, and the arguments that bind a generic type parameter. All six use one rule.

  1. The target first. Where the context says what type the result has (a declaration, an assignment, an argument, a return, a field), each arm is checked against that type and nothing is merged: int | string x = c ? 1 : "s";, Array<int | string> xs = [11, "s"];, P? r = c ? P() : None;, float f = c ? 1 : 2.5;.
  2. Else the most specific common supertype of the arms. Dog and Animal give Animal; Dog and Cat (both : Animal) give Animal.
  3. Else a compile error that names the place and asks for the type.
arms result
1, 1 int
f : float, 1 float (the literal adopts the typed arm)
1, 2.5 error
Dog, Animal / Dog, Cat Animal
od : Dog?, Animal Animal? (an optional arm lifts the result)
P, Q (unrelated) / P, None / 1, "s" error

Rules

  • Numbers are not widened. Two different numeric types have no common type (int with float, int8 with int16). A bare numeric or character literal beside a typed arm takes that arm's type: c ? f : 1 is a float, c ? ch : 'a' is a char. Arithmetic (i + f) keeps its own working-type rule and is not a merge.
  • No union is formed. The one exception is optional lifting: an arm that is already optional (Dog?) makes the result optional. P beside a bare None is an error; write the target, P? r = c ? P() : None;.
  • a ?? b merges a without None with b: Dog? ?? Animal() is an Animal, Array<int>? ?? [] is an Array<int> ([] takes the typed side), and int? ?? "s" is an error.
  • An empty array literal [] has no elements to merge: it is the raw Array until a target gives it a type.
  • A match arm with a block body gives no value and takes no part in the merge.
  • A conditional or match whose value is discarded (a statement such as match (cmd) { "inc" => n += 5; else => console.writeln("?"); }) is not merged: its arms are statements.
  • A call argument is typed against the parameter of the overload that is chosen, arm by arm: f([P(), 3]) with void f(Array<P | int> xs) compiles. Against void f(Array<P> xs) the error names the element that does not fit. Among overloads, an argument whose arms have no common type selects the candidates whose parameter takes every arm, ranked by the weakest arm's conversion: with g(byte) and g(int), g(c ? aByte : anInt) calls g(int); with f(int) and f(float), f(c ? anInt : aFloat) is ambiguous.
  • A lambda with no target and several differing returns merges them. When they share no type, write the lambda's return type: Foo? (int k) => { if (k > 0) { return Foo(); } return None; }.
  • Generic type arguments are inferred by the same rule from the arguments (lang.generics).

The errors:

  • the arms of this conditional have no common type: 'int' and 'string' have no common supertype; write the type it should have
  • the arms of this match have no common type: ...
  • the elements of this array literal have no common type: ...; write the array's type, as in 'Array<int | string> xs = [...]'
  • the two sides of '??' have no common type: ...
  • the returns of this lambda have no common type: ...; write its return type, as in 'Foo? (int k) => { ... }'

Examples

Each merge site

use io::console;

class Animal { string name() => "animal"; }
class Dog : Animal { string name() => "dog"; }
class Cat : Animal { string name() => "cat"; }

void main() {
    bool c = false;
    float f = 2.5;

    var pet = c ? Dog() : Cat();
    console.writeln(pet.name());

    var level = c ? f : 1;
    console.writeln(level);

    var kind = match (2) { 1 => Dog(), else => Animal() };
    console.writeln(kind.name());

    var pets = [Dog(), Cat()];
    for (Animal a in pets) console.writeln(a.name());

    Dog? missing = None;
    console.writeln((missing ?? Animal()).name());

    var make = (int k) => { if (k > 0) { return Dog(); } return Cat(); };
    console.writeln(make(1).name());

    int | string either = c ? 1 : "one";
    console.writeln(either);
}
main();
cat
1.0e0
animal
dog
cat
animal
dog
one

See also

  • Generics — Generic classes and functions, how type arguments are inferred or given explicitly with ::<T>, invariance, how each instantiation is checked, T::member and type constructors.
  • Unions and optionals — Closed unions T | U, the optional T?, narrowing with != None and is, and the ?? and ?. operators.
  • Numeric conversions and working types — Values cross between integer and float types only through a conversion call, lossless widening is implicit, and literals take the type their context asks for.