-
Notifications
You must be signed in to change notification settings - Fork 1
Variables
A variable in MScript is declared as follows:
foo = "bar"
MScript is a type-safe language, which means that for the duration of its scope and any child scopes, the name foo
must always be a string.
foo = "bar" foo = 5--> ./variables.ms:2:7 | 2 | foo = 5 | ^ | = type mismatch: this assignment will update a variable with type `int`, which is not compatible with the original type `str` Error: Did not compile successfully (1 Error)
Generally, MScript can infer the type of an assignment based on the type of the to-be-assigned value. However, you may explicitly define a type as follows:
planets: int = 8
name: str = "Tom"
# this is okay! we're sticking with the same types
planets = 9
name = "Jerry"
This modifier ensures that a variable will never be reassigned somewhere else in code.
const name = "Bard" name = "zzzz"--> ./try_to_crash.ms:2:8 | 2 | name = "zzzz" | ^----^ | = attempting to reassign to a const variable Error: Did not compile successfully (1 Error)
It does not freeze the contents of a mutable object, like a list or class.
const list: [int...] = [1, 2, 3]
list.push(4)
list[0] = 10
assert list == [10, 2, 3, 4]
This modifier includes this variable in the file's module and makes it accessible from other .ms
files. An assignment that is "exported" must explicitly define a type.
- This variable can be modified anywhere; be careful what you expose:
export version: str = "v1.0.0"
- This variable can be read and copied anywhere, but not modifed:
export const version: str = "v1.0.0"
This modifier is used to update a variable visible to a function, but not in its immediate scope. The keyword is complex in nature, and likely used scarcely in real code. See the section about closures and captured variables for a more detailed breakdown of how this keyword operates.