Skip to content

MS2 Type safety

GoogleFeud edited this page Mar 14, 2021 · 6 revisions

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.

What is completely type-safe?

✔️ Imported / Exported variables
✔️ Variables
✔️ Objects / Structures
✔️ Function arguments / return values
❌ Arrays

How implicit types work

Variables

Structure fields work in the same way.

const

Here a is of type number and can never change it's value or it's type.

const a = 5;

let

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; // Error

In 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; // Error

Functions

At 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! 

How to provide typings for your imports / exports

A type looks like this:

{
id: number, 
extends?: number,
properties?: Record<string, Typing|number>,
parameters?: Array<Typing|number>,
returns?: Typing|number,
nullable?: boolean
}

Built-in types

UNKNOWN: 0,
NULL: 1,
NUMBER: 2,
STRING: 3,
BOOLEAN: 4,
ARRAY: 5,
FUNCTION: 6

Clone this wiki locally