Variable and Constant Declarations

Constant Declaration

Whenever declaring a new constant, you must specify the type of the constant and then use = operator to assign values:

Num foo = 123
const foo = 123;

Constants forbidden reassignment:

Num foo = 123
foo = 456

The sample code above throws error:

ParserError: Constant `foo` cannot be reassigned

Variable Declaration

Wyrd introduce a keyword mutable in order to declare variables:

mutable Num foo = 123

It will compiled into JavaScript with let variable declaration:

let foo = 123;

Hence, reassignment is now permitted:

mutable Num foo = 123
foo = 456
let foo = 123;
foo = 456;

And of course, if you already declared a constant, redeclared as mutable variable throws error:

Num foo = 123
mutable Num foo = 456
ParserError: Constant `foo` cannot be redeclared as variable

"maybe" Types Declaration

There are cases where you might need a variable to assign value represents the concept of empty. In Wyrd, everything about empty is represented as a primitive type of value Null.

On the other hand, Wyrd introduced maybe types which can store either Null value or declared type. However, since the declared variable should have mutability, not only the keyword maybe involves, but also mutable keyword as well:

mutable maybe Num foo = 123
foo = Null
foo = 456
let foo = 123;
foo = null;
foo = 456;

Since mutable variables can also be assigned Null, instead of directly assigned with Null, we can just omit the assignment and skip to next line, it will automatically assign the maybe type with Null value.

mutable maybe Num foo
let foo = null;

Last updated

Was this helpful?