Skip to main content

Block Scopes

Curly braces {} and parentheses () and are used to create block scopes in Easel. A block scope can be used to execute multiple statements in sequence. Any variables declared inside a block scope will be deallocated when the block ends. In general, use parentheses for expressions that return values, and curly braces for statements that do not return values.

Curly Braces {}

Use curly braces to create a new scope. When the scope ends, all variables inside of it will be deallocated and will no longer be accessible.

pub game fn World.Main() {
let a = 2, b = 3
{
let c = a + b
DoSomethingWith(c)
}
// Variable `c` is not accessible here
}

Parentheses ()

Use parentheses to determine the order of operations when performing a calculation:

let x = 1 + 2 * 3
let y = (1 + 2) * 3
// x is 7, y is 9

Multiple Statements

To execute multiple statements in parentheses, simply put each statement on a separate line. The last expression in the block determines the resulting value of the parentheses.

let x = (
let a = 1
let b = 2
Print { "Hello, world!" }
a + b
)
// x is now 3

You can use semicolons to separate statements on the same line. This is the same example as above, but with semicolons instead of newlines:

let x = (let a = 1; let b = 2; Print { "Hello, world!" }; a + b)

Parentheses define a new scope, so after the parentheses end, all variables declared inside of it will be deallocated and will no longer be accessible:

let x = (
let a = 1
a + 123
)
let y = a + 456 // Error: unknown identifier `a`

Returning Multiple Values

Use commas to return multiple values from a parentheses block:

let x, y = (1, 2 + 3)
// x is 1, y is 5

You can have multiple statements before the final expression when returning multiple values as well:

let x, y = (
let a = 1
let b = 2 + 3
a, b
)
// x is 1, y is 5

When to Use Parentheses or Curly Braces

tip

When you want to return a value, use parentheses. When you do not want to return a value, use curly braces.

Curly braces and parentheses are the same except for two differences.

Nested Curly Braces

Braces and parentheses interpret a nested set of curly braces {} differently:

  • When inside a set of parentheses (), a set of curly braces {} will be interpreted as a Map literal.
  • When inside a set of curly braces {}, a set of curly braces {} will be interpreted as another block scope.
pub game fn World.Main() {
let a = 2, b = 3

// Use parentheses to create a block scope inside an expression
let output = (
let c = a + b
DoSomethingWith(c)
c // The last expression determines the resulting value
)

// Braces in an expression create a Map, which is why you have to use parentheses instead of braces
let x = {
apple = 123,
banana = 456,
}

// This is a block scope - no return value
{
let c = a + b
DoSomethingWith(c)
}
}

Returning Multiple Values

Only parentheses can be used to return multiple values.

pub game fn World.Main() {
// Parentheses can return multiple values, separated by commas
let x, y = (123, 456)

// Parentheses can have multiple statements before the final expression when returning multiple values
let x, y = (
let a = 100
a, 2*a
)

// Must use parentheses inside of curly braces to return multiple values:
let x, y = if true {
(123, 456)
} else {
(789, 404)
}

// The following is an error because curly braces in an expression define a Map literal:
let x, y = { 123, 456 }
}