Expressions
Strictness
What the compiler rejects instead of guessing, and which failures are left to run time as catchable exceptions.
since 0.1.0-alpha.1linuxwindowswasm
Description
Leviathan prefers a compile error to a guess. A program that names something that does not exist, leaves a generic type argument without a source, or mixes unrelated types in a declaration is rejected before it runs. What the compiler cannot decide ahead of time fails at run time with a RuntimeException that a program can catch.
Failures that surface at run time are catchable
use io::console;
Array<int> xs = [1, 2, 3];
try {
console.writeln(xs[5]);
} catch (RuntimeException e) {
console.writeln("array: ${e.message}");
}
try {
int zero = 0;
console.writeln(10 / zero);
} catch (RuntimeException e) {
console.writeln("math: ${e.message}");
}
Map<string, int> m = Map();
try {
console.writeln(m["missing"]);
} catch (RuntimeException e) {
console.writeln("map: ${e.message}");
}
array: index 5 out of bounds (length 3)
math: division by zero
map: key not found: missing
Rules
- An unknown name or function is a compile error.
- A generic construction whose type argument has no source is a compile error. Provide a target type, an argument that carries the type, or an explicit construction tuple such as
Box::<int>(). A type argument is inferred when it can be recovered, written explicitly when you want to, and required when it cannot be recovered. - Explicit type arguments on a call must match the number of type parameters exactly and are authoritative. A value that conflicts with a type the explicit arguments pin is a compile error at that argument.
- A method the receiver's type does not declare is a compile error:
n.length()on anintreportstype 'int' has no method 'length', andb.nosuch()on aBoxreportstype 'Box' has no method 'nosuch'. This holds for every primitive, class, struct, enum and interface type, library classes at any instantiation included (Set<int>has noadd,Array<string>has nopush). The type's methods are its own, inherited and interface methods, its accessors, its fields (a field may hold a function), and the members rules inject. A base-typed or interface-typed receiver offers only the methods of that type, even when the object at run time has more: narrow it first withisor a type arm. A narrowed value keeps the methods of its declared class as well as those of the type it was narrowed to. - A receiver whose type is not known at compile time — a value of a type parameter, an untyped value — is not checked: a missing method on it fails at run time.
- A prefix operator on the wrong primitive is a compile error:
!outsidebool,-outside the numeric types,~outside the integer types. Seelang.prefix-operators. - An operator an object's type does not declare is a compile error:
p + pon a class with no(+)member reportsno operator '+' on 'Plain', and a prefix-,!or~on any object is rejected the same way.==and!=on a class need no declared member: they compare identity. Seelang.operator-overloading. - A value of one kind is not accepted where an unrelated kind is declared:
string s = 5;,int y = "5";,bool b = 1;andint n = 1 + "2";are compile errors. - Runtime failures throw a
RuntimeExceptionthat can be caught: an index out of bounds,m[k]orm.at(k)on a map with a missing key, an integer division by zero, and a call whose target cannot be resolved.
Examples
use io::console;
console.writeln(undefinedName);
// error: unknown name 'undefinedName'
undefinedFunction(3);
// error: unknown function 'undefinedFunction'
class Box<T> { T v; }
Box b = Box();
// error: cannot infer type argument 'T' for 'Box'; provide a target type or a type-bearing argument
string s = 5;
// error: cannot initialize 's' with 'int'
int n = 3;
n.length();
// error: type 'int' has no method 'length'
Set<int> seen = Set();
seen.add(1);
// error: type 'Set<int>' has no method 'add'
class Plain {
int v = 1;
}
Plain p = Plain();
console.writeln(p + p);
// error: no operator '+' on 'Plain'
Providing the type argument explicitly
use io::console;
class Box<T> {
T value;
}
Box<int> a = Box::<int>(5);
console.writeln(a.value);
5
Notes
Numeric values adopt the declared numeric type at a declaration, so float z = 3; holds 3.0, and conversions between numeric types follow the numeric-conversion rules. Conditions in if and while are meant to be bool; write an explicit comparison such as n != 0 rather than relying on a number as a condition.
See also
- RuntimeException — The error the standard library throws when an operation fails at run time.
- Prefix operators — The operand types accepted by the unary operators !, - and ~, and the results they produce.
- Operators on objects — A class defines the meaning of an operator by declaring a member with a symbolic selector; the equality operator derives its negation automatically.