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.
- 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;. - Else the most specific common supertype of the arms.
DogandAnimalgiveAnimal;DogandCat(both: Animal) giveAnimal. - 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 (
intwithfloat,int8withint16). A bare numeric or character literal beside a typed arm takes that arm's type:c ? f : 1is afloat,c ? ch : 'a'is achar. 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.Pbeside a bareNoneis an error; write the target,P? r = c ? P() : None;. a ?? bmergesawithoutNonewithb:Dog? ?? Animal()is anAnimal,Array<int>? ?? []is anArray<int>([]takes the typed side), andint? ?? "s"is an error.- An empty array literal
[]has no elements to merge: it is the rawArrayuntil a target gives it a type. - A
matcharm with a block body gives no value and takes no part in the merge. - A conditional or
matchwhose value is discarded (a statement such asmatch (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])withvoid f(Array<P | int> xs)compiles. Againstvoid 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: withg(byte)andg(int),g(c ? aByte : anInt)callsg(int); withf(int)andf(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 havethe 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::memberand type constructors. - Unions and optionals — Closed unions
T | U, the optionalT?, narrowing with!= Noneandis, 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.