Return to homepage

DOSATO

Copyright © 2024 Sebastiaan Heins

1.6 Extensions and loops

Extensions

Extension keywords are used to extend the functionality of the do keyword.
There are 8 extensions:

when
if
else
while
for
then
catch

Extensions are used to create conditional statements, loops and error handling.
Extensions are always followed by some kind of expression or a block of code.

when else

when is used to create a conditional statement.
when is followed by a boolean expression, a block of code and an optional else block.
The boolean expression is evaluated and if it is true, the block of code is executed.
If the boolean expression is false, the else block is executed.
Example of a when statement with an else extension:

do {
    do say ("Access granted")
} when (password == "1234") else {
    do say ("Access denied")
}

when encapsulates the entire block of code, if the condition is true, the block is executed.
Otherwise, nothing before it is executed.

do say ("Will not run") then say ("Will also not run") when (false)

When doesn't need the parentheses.

do say ("Will run") when true

while loops

while is used to create a loop.
while is followed by a boolean expression and a block of code.
The boolean expression is evaluated and if it is true, the block of code is executed.
The boolean expression is evaluated again and if it is true, the block of code is executed again.
This process repeats until the boolean expression is false.
Example of a while loop:

make int i = 0
do {
    do sayln (i)
    set i++
} while (i < 10)

This loop will print the numbers 0 to 9 to the console.
The variable i is incremented by 1 each time the loop is executed.
The loop will stop when i is equal to 10.

Just like with when, while doesn't need the parentheses, and encapsulates the entire block of code.

make int i = 0
do sayln (i) then {set i++} while i < 10

for

make int i = 0
do sayln (i) for (range(100) => i)

for loops are used to iterate over an array, in this example, the range(100) function returns an array of numbers from 0 to 99.
The for loop will iterate over the array and assign the current value to the variable i.
The variable i is then printed to the console.
You can use the _ variable after the => to discard the value, and just loop as many times as the length of the array

do sayln ("Hello world!") for (range(10) => _)

then

then is used to immediately execute a function after the previous function has finished.
then is followed by a function call.
Example of a then statement:

do say ("Hello world!") then say ("Hello again!")

This will print Hello world! and then Hello again! to the console.
The second say function is executed after the first say function has finished.

if

do if (condition) then sayln ("Access granted") else sayln ("Access denied")

if is used to create a conditional statement.
if is followed by a boolean expression, a then extension and an optional else extension.
The boolean expression is evaluated and if it is true, the block of code is executed.
If the boolean expression is false, the else block is executed.
If only encapsulates the next statement, not the entire block.

do if (true) then sayln ("Will print when the condition is true") then sayln ("Will always print regardless")

You can chain else and ifs together.

make bool result = listen("True or False?\n") == "true" 
do if result then {
    do say("Hello, World!")
} else if !result then {
    do say("Goodbye, World!")
} else {
    do say("This should never happen.")
}

catch

catch is used to catch errors.
catch is followed by a block of code.
Example of a catch statement:

do {
    do say (undfined_variable) // This will throw an error
} catch {
    do say ("An error occured!\n Code: " + _)
}

If an error occurs in the block of code, An error occured! will be printed to the console.
The error code is stored in the _ variable.
More information about error handling can be found in 1.10 Error handling.

Extension chaining

Extensions can be chained together.
Example of a chained statement:

do {
    do say ("Access granted")
} then login() when (password == "1234") else {
    do say ("Access denied")
}

This will call the login function after the say function has finished.
This only happens when when is true, the when statement includes the entire block
If the password is not 1234, Access denied will be printed to the console.

Info!
You can learn more about extensions in 2.3 do extensions