Basics
Constants
Constants are named values that can never be reassigned.
To declare a constant we write name: value
. For example:
pi: 3.1415
We can now use pi
wherever we would like to use 3.1415
.
Variables
Variables are named values that can be reassigned. Once a variable is declared its type can never be changed.
There are three ways to declare a variable:
Write
name: Type
to specify the variable's type without assigning it a value:count: Int
If we try to use
count
before assigning it a value we will get an error.Write
name: Type = value
to specify the variable's type and also assign it an initial value:ready: Bool = true
Write
name := value
to specify only an initial value and have the type be inferred:quote := "Not all those who wander are lost."
Above,
quote
took the type of the value we assigned to it, which in this case isString
.We can assign a new value to
quote
, as long as it is also of typeString
:quote = "There is nothing permanent except change."
Lists
A list is an ordered collection of values. To create a list we use square brackets:
somePrimes: [1, 2, 3, 5, 7, 11]
emptyList: []
multi: [1, "hi", SYMBOL]
List elements can be accessed via indexing:
somePrimes[0] # The first element of `somePrimes`
multi[2] = "cat" # Update the third element of `multi`
To append an element to the end of a list use push
:
multi.push(🔥)
Objects
An object is an unordered collection of named values. To create an object we specify its properties' names and values inside parentheses:
person := (name: "Daniel", age: 29)
Object properties can be accessed and updated:
person.name # "Daniel"
person.age = 30 # Update person.age
Functions
A function is a reusable block of code. To create a function we specify its arguments in parentheses, followed by an arrow, followed by the optional return type and the function body:
add: (a, b) -> { a + b }
addInts: (a: Int, b: Int) -> Int { a + b }
The parentheses are always necessary. If the function body is only a single expression then the return type and braces can be omitted:
hello: (name: String) -> print("Hello @name!")
To call the functions:
result := add(1, 2) # 3
hello("Flame") # prints "Hello Flame!"