Primitives
Listed below are all the different types that Loop has out of the box. All the types have an explanations and some examples.
An integer is a primitive value that can be up to 64bits in length. You can define an integer like this:
integer := 10; // Positive integer
integer := -200; // Negative integer
// Returns 123 to string
123.to_string()
A boolean is a primitive value that can either be true or false. You can define a boolean like this:
boolean := true
boolean := false
This method will cause an exception if the string is unable to be converted.
- true = 1
- false = 0
// Converts the boolean true to 1
boolean :true
boolean.to_int()
A string is a sequence of characters inside
"
's. You can define a string like this:str := "hello" // A single word
str := "hello world" // Multiple words
This method will cause an exception if the string is unable to be converted.
// Converts the string "123" to integer
"123".to_int()
Loop only has one primitive indicating the absence of a value. Which is "null", a Null value indicates "not a value". You can define something to be null like this:
has_nothing := null
The Null primitive will result in "false", for example.
has_nothing := null
if (has_nothing) {
// Will not print
println("Hi!")
} else {
// This will print
println("Bye!")
}
An array is a list of values. Arrays are automatically dynamic but can only have one type in them.
array := [100, 50, 300]
// Wont work!
array := ["hello", "world", "!"]
Accessing an array is simple. You use the index operators (square brackets []). Let's take the previous example and use it here to access the "100" value in the sub array.
array := [[100], [300]]
value := array[0][0] // sets the value of "value" to 100
Or a simpler array:
array := [100, 200, 300]
value := array[1] // sets the value of "value" to 200
// Initialize new array
x := [0, 1]
// Add 2 to the array
x.add(2)
// Array will now be [0, 1, 2]
x
// It's also possible to add multiple to the array at once
x.add(3, 4, 5)
// Array will now be [0, 1, 2, 3, 4, 5]
x
x := [0, 1, 2]
// Removes index 0 from the array
x.remove(0)
// Removes index 1 from the array
x.remove(1)
// Array will now be [1]
x
// Initialize array with 3 elements
x := [0, 1, 2]
// Returns 3
x.length()
A hashmap is a way to bind keys to values. The key is limited in what types they can be, currently these types are allowed as keys:
- String
- Integer
- Boolean
Hashmap keys are required to be unique, they can have spaces as well (if you're using a string as key).
Creating a hashmap is easy and might seem familiar if you've used other languages before. An example with a few sample key value pairs is as follows:
hashmap := {
"key1": "value",
"key2": "value"
}
Important: the key and value types can only be specified once, and require to be the same afterwards.
Last modified 1yr ago