Skip to content

MS2 Type safety

GoogleFeud edited this page Mar 12, 2021 · 6 revisions

MS2 is 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.

How implicit types work

Variables

Structure fields work in exactly 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. The second line will throw an error.

let a = 5;
a = "Hello";

In this case a is nullable. It can be null or the first value that gets assigned to it. So null|number. The third line will throw an error.

let a;
a = 5;
a = "Hello!";

Arrays

Arrays can contain values of multiple types, but there are a few rules.

Array literals which have values inside are considered static - they cannot be written to, only read.

const arr = [1, 2, "a"];

Import types

You should always provide typings to the objects you import. For example:

const Evaler = new MS2({
   typings: {
     imports: {
      player: { type: OBJECT, properties: { name: STRING, id: NUMBER } },
      someArr: { type: ARRAY, of: [STRING, NUMBER], readonly: true }
} 
}
});

Export types

Same for the export types. In order to make sure you are getting all the exports you need, specify them:

const Evaler = new MS2({
   typings: {
     exports: {
        name: STRING,
        someHook: {type: FUNCTION, returns: BOOLEAN, params: [STRING, NUMBER], allowNull: false}
} 
}
});

All typings settings

  • type - The type
  • returns - Only applicable to functions, the return type of the function
  • params - Only applicable to functions, the parameters of the function
  • allowNull - If the import/export can be null
  • properties - Only applicable to objects, the properties of the object
  • of - Only applicable to arrays, the types of the values inside the array
  • readonly - Only applicable to imported variables, if the variable can be overridden
  • locked - Only applicable to imported objects, if any of the properties of the object can be modified

Clone this wiki locally