Types
Type expressions
How a type is written: names, qualified names, generic instantiations, unions, optionals and function types.
since 0.1.0-alpha.1linuxwindowswasm
Description
A type expression appears wherever a type is expected: a variable declaration, a field, a parameter, a return type, a type argument. These forms exist:
| form | example | meaning |
|---|---|---|
| name | int, string, MyClass |
a built-in or declared type |
| qualified name | Shapes::Box |
a type declared in a namespace |
| generic instantiation | Array<int>, Array<Array<int>> |
a generic type with its type arguments |
| union | int | string |
a value that is one of the listed types |
| optional | int? |
shorthand for int | None |
| function type | (int, int) => int |
a function taking two ints and returning int |
var stands where a type would in a declaration. It is not a type: it asks the compiler to use the type of the initializer, and the variable can be reassigned with values of that type. To fix a variable after its initializer, spell its type with const (const int n = 5;).
Type expressions in declarations
use io::console;
namespace Shapes {
class Box {
int side = 2;
}
}
class Couple<A, B> {
A first;
B second;
}
int apply((int, int) => int op, int a, int b) => op(a, b);
Shapes::Box b = Shapes::Box();
Array<Array<int>> grid = [[1], [2, 3]];
Couple<string, int> p = Couple("k", 7);
console.writeln(b.side);
console.writeln(grid.length());
console.writeln(p.second);
console.writeln(apply((x, y) => x * y, 6, 7));
2
2
7
42
Rules
- A type name is an identifier, optionally qualified with
::. A generic type adds its arguments in angle brackets:Name<T1, T2>. Instantiations nest:Array<Pair<K, V>>. - A union
T1 | T2is a closed set of alternatives.T?isT | None. Seelang.unions-and-optionals. - A function type is written
(T1, T2) => R, with the parameter types in parentheses and the result type after=>. - A function value fits a function type when it takes the same number of parameters, every parameter type of the target fits the value's parameter at the same position, and the value's result type fits the target's result type. Parameters are contravariant and the result is covariant: a function taking an
Afits(B) => intwhenBis a subclass ofA, a function takingint | stringfits(int) => int, and a function returning aBfits() => A. The rule applies wherever a value is bound: a declaration, an assignment, an argument, areturn, a field, an optional slot((int) => int)?and a collection element. - Numeric parameter and result types must be the same type. No numeric conversion happens through a function type, so a function taking a
floatdoes not fit(int) => float. - A
voidresult fits onlyvoid, and a non-voidresult does not fitvoid. A lambda literal with an expression body is the exception: it is typed against its target, and in avoidtarget its value is discarded. A lambda held in avarhas no target: it is a function value of the type it was given (lang.primary-expressions), sovar p = (int a) => a + 1;does not fit(int) => void, and neither doesint (int a) => a + 1. - A lambda's written return type is checked against its target's result by the same rule:
(int) => Animal f = Dog (int x) => Dog();fits, and(int) => float g = int (int n) => n;is a compile error. - Default parameter values are not part of a function type.
int add(int a, int b = 2)has the type(int, int) => int, so it does not fit(int) => int. - A function value is not any other type:
int n = f;is a compile error. varis an inference marker, never part of a type. The variable's type is the initializer's type. It is written only in a local declaration (a function, method, constructor, accessor or lambda body, a block, aforloop variable or initializer), at a script's top level, in a namespace body, or aftercomptime.int f(var x),Array<var>,var?, a fieldvar x = 1;andcatch (var e)are compile errors.- The initializer must have a type on its own:
var x;,var xs = [];,var t = Foo;(a type, not a value) and an initializer whose type is not known (var e = id([]);) are compile errors that name the fix, such asArray<int> xs = [];. const varandusing varare compile errors:constandusingspell their type.
Examples
Unions, optionals and inference markers inside a function:
Unions, optionals and var
use io::console;
void demo() {
int | string either = 5;
int? maybe = None;
var v = 10;
const int w = 20;
v = v + 1;
console.writeln(either is int);
console.writeln(maybe == None);
console.writeln(v + w);
}
demo();
true
true
31
A function-typed variable:
A function type
use io::console;
void demo() {
(int) => int inc = (x) => x + 1;
console.writeln(inc(4));
}
demo();
5
Function values convert along the parameter and result types:
Function type conversions
use io::console;
class Animal {
string name() => "animal";
}
class Dog : Animal {
string name() => "dog";
}
namespace Zoo {
string describe(Animal a) => "it is a " + a.name();
Dog adopt(int n) => Dog();
int width(int | string v) => v is int ? 1 : 2;
}
void demo() {
(Dog) => string d = Zoo::describe;
(int) => Animal a = Zoo::adopt;
(int) => int w = Zoo::width;
console.writeln(d(Dog()));
console.writeln(a(1).name());
console.writeln(w(5));
}
demo();
it is a dog
dog
1
Notes
A var and a variable whose type is a function type ((int) => int f = (int n) => n + 1;) can both be declared at the top level of a script.
See also
- Array — An ordered sequence of values of one type,
Array<T>, with value semantics.