-
Notifications
You must be signed in to change notification settings - Fork 1
MS2 Type safety
MS2 is mostly a type-safe programming language, but it's type safety features are implicit. That means that the end-user does not manually specify the type of variables, parameters or return types. Those are all handled under the hood.
✔️ Imported / Exported variables
✔️ Variables
✔️ Objects / Structures
✔️ Function arguments / return values
❌ Arrays
Structure fields work in the same way.
Here a is of type number and can never change it's value or it's type.
const a = 5;a is initially declared as a number, so it's type is locked to number.
let a = 5;
a = "Hello"; // Error
a = 10; // Valid
a = null; // ErrorIn this case a is nullable. It can be null or the first value that gets assigned to it. So null|number in this case.
let a;
a = 5;
a = "Hello!"; // Error
a = 10; // Valid
a = null; // ErrorAt first, the types of the function arguments and return value are unknown, but once the function is called with the correct amount of arguments, the types of the arguments and the return type get locked in:
const fn = (a, b, c) => a + b + c; // a, b, c's types are unknown, the body doesn't get type-checked at all.
fn(1, 5, true); // ERROR! A type looks like this:
{
id: number,
extends?: number,
properties?: Record<string, Typing|number>,
parameters?: Array<Typing|number>,
returns?: Typing|number,
nullable?: boolean
}UNKNOWN: 0,
NULL: 1,
NUMBER: 2,
STRING: 3,
BOOLEAN: 4,
ARRAY: 5,
FUNCTION: 6