Assignments

Home of the ABS programming language: the joy of shell scripting.


Assignments

Just like about any other language, assignments are pretty straightforward:

x = "hello world"

Array destructuring is supported, meaning you can set multiple variables based on an array:

x, y, z = ["hello world", 99, {}]
x # "hello world"
y # 99
z # {}

If the number of variables you’re trying to set is longer than the array, the extra variables will be set to null:

x, y = [1]
y # null

ABS doesn’t have block-specific scopes, so any new variable declared in a block is automatically available outside as well:

if true {
    x = "hello world"
}

echo(x) # "hello world"

Variables declared in native expressions, such as for loops, are the only exception to the rule, as they get “cleared” as soon as the expression is over:

for x in 1..10 {
    echo(x) # 1, 2, 3...
}

echo(x) # Error: x is not defined

Worth to note that if a variable gets re-defined within these expressions, it will temporarely assume its new value, but will rollback to the original one once the expression is over:

x = "hello world"

for x in 1..10 {
    echo(x) # 1, 2, 3...
}

echo(x) # "hello world"

Next

That’s about it for this section!

You can now head over to read about returning values.