Return to homepage

DOSATO

Copyright © 2024 Sebastiaan Heins

2.3 extensions

In this section, we will discuss the extensions to the do function that are provided by the DOSATO standard library.
We will take a closer look at all the extensions.

if

if is an extension to the do function, it is used to create a conditional statement.
if is followed by (), then right after a then block and an optional else block after the full then.
The expression inside the () is evaluated and if it is true, the then block is executed.
If the expression is false, the else block is executed.
Heres some example code:

do if (password == "1234") then say ("Access granted") else say ("Access denied")

This code will check if the variable password is equal to "1234".
If it is, it will print "Access granted", otherwise it will print "Access denied".
This is a simple example of an if statement.
if statements only count for the first then in the chain, the rest of the THENs are ignored.
Example of this:

do if (password == "1234") then say ("Access granted") then say ("Started!")

The code above will print "Access granted" when true and then "Started!".
"Started!" will always be printed, even if the expression is false, this is because the second then is ignored.

when

when is the second way to create a conditional statement.
when is followed by (), when is placed at the end of a statement, right after can follow an else.
The expression inside the () is evaluated and if it is true, everything before the when will be excecuted.
If the expression is false, the else block is executed.
Heres some example code:

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

This code will check if the variable password is equal to "1234".
If it is, it will print "Access granted", otherwise it will print "Access denied".
This is a simple example of a when statement.
Unlike the if statement, when statements contains the entire chain before.
Example of this:

do say ("Access granted") then say ("Started!") when (password == "1234")

The code above will print "Access granted" when true and then "Started!".
"Started!" will also only be printed when the expression is true, if it is false, nothing will be printed.
when works differently than if, when is used to create a conditional statement at the end of a chai, and contains the entire chain before.

else

else is used to create an alternative block of code that is executed when the expression in the if or when statement is false.
else is always placed after the then block or directly after the when extension.
else is optional, if it is not present, nothing will be executed when the expression is false.
Example of an if statement with an else block:

do if (password == "1234") then say ("Access granted") else say ("Access denied")

This code will check if the variable password is equal to "1234".
If it is, it will print "Access granted", otherwise it will print "Access denied".
This is a simple example of an if statement with an else block.
Example of a when statement with an else block:

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

This code will check if the variable password is equal to "1234".
If it is, it will print "Access granted", otherwise it will print "Access denied".
This is a simple example of a when statement with an else block.

for

for is used to create an array iteration loop.
for is placed at the end of a call chain.
for is followed by (), in the () you put the array you want to iterate over, then the => operator and after that the variable name for the element.
Example of a for loop:

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

range(100) returns an array of numbers from 0 to 99.
The for loop will iterate over the array and print each element of the array to the console.
The variable i is used to store the current element of the array.
The for loop will stop when there are no more elements in the array, or a break is called.
Make sure the variable exists before the for loop, or it will throw an error.
You can also discard the iterator of the array by using the _ variable.

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

You can iterate over any array, not just the range function.
Example of this:

make array names = ["Robotnik", "Alex"]
make i = null
do sayln ("Hi " + i) for (names => i)

You can break out of a for loop by using the break keyword.
Example of this:

make  i = 0
do {
    do sayln (i)
    break when i == 5
} for (range(10) => i)

Or you can continue to the next iteration of the loop by using the continue keyword.

make  i = 0
do {
    do sayln (i)
    continue when i % 2 == 0
} for (range(10) => i)

while

while is used to create a loop.
while is placed at the end of a call chain.
while is followed by ()
The expression inside the () is evaluated and if it is true, the block of code is executed.
The expression is evaluated again and if it is true, the block of code is executed again.
This process repeats until the expression is false.
Example of a while loop:

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

This loop will print the numbers 0 to 99 to the console.
The code inside the block is executed until the expression i < 100 is false.
The variable i is incremented by 1 each time the loop is executed.
So the loop will stop when i is equal to 100.

Just like with for, you can use the break and continue keywords to break out of the loop or continue to the next iteration.
Example of this:

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

This will print the numbers 0 to 4 to the console.
The loop will stop when i is equal to 5.

then

then is used to immediately execute a function after the previous function has finished.
then is placed after a call or an if.
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.

catch

catch is used to catch errors that are thrown by a function.
catch is placed after a function call.
catch is followed by a block of code.
Example of a catch statement:

do say (thisVarDoesNotExist) catch {
    do say ("Error!")
}

This will print Error! to the console.
The catch block is executed when the say function throws an error.
When it does, it stops the call chain and executes the catch block.
The program will not crash, it will continue after the catch block.
The error code will be stored in the _ variable.
Example of this:

do say (thisVarDoesNotExist) catch {
    do say ("Error: " + _)
}

This will print Error: 47 to the console.
The _ variable contains the error code of the error that was thrown.
The _ variable is a special variable that stores the error message. (and much more)
More info about the _ variable can be found in the 2.4 The _ variable

Info!
The catch block only catches runtime errors, not syntax errors.