Other expressions

Case expressions

A case expression selects one of several values based on some condition. A case expression consists of at least one case clause and exactly one default case beginning with the else keyword.

let x = random(0, 1);

// Example: flip a virtual coin.
return case when x > 0.5 then "heads"
            when x < 0.5 then "tails"
            else "standing on edge"
            end;

Function calls

FlowScript comes with a number of built-in functions, and you can define your own.

// Example: call the "upper" function with "hello" for its single argument.
return upper("hello");

Default

Sometimes it is useful to create en "empty" value of some type. You can use the default keyword for this.

type User = {name: text, age: number, email: text?};

return default(User);

Error

The error expression (which doubles as a statement) stops the execution of the application and displays an error message to the user.

if quantity <= 0:
    error "Quantity must be greater than zero."

Like

The like operator is used for simple, SQL-like matching of text values.

return "sweden" like "swe%"; // true
return "sweden" like "norw%"; // false

The value on the right side is referred to as the "pattern".

Wildcards

Wildcards are used to substitute any number of characters in text comparison.

  • %: Matches any sequence of characters, including zero characters.

    • 'sweden' like 'swe%' matches because anything can follow "swe".

    • 'austria' like '%ia' matches because the string ends with "ia".

  • _: Matches exactly one character.

    • 'luxembourg' like 'l_xembour_' matches because "u" and "g" are the single characters in those positions.

Character Sets

Character sets allow you to specify a set of characters, any of which can match a character in the specified position.

  • [...]: Matches any single character within the brackets.

    • 'myanmar' like 'my[a-z]nmar' matches because "a" falls within the range "a" to "z".

    • 'USA' like 'US[AB]' matches because "A" is in the set "A, B".

  • [character-character]: Specifies a range of characters.

    • 'myanmar' like 'my[a-z]nmar' uses a range to specify any letter from "a" to "z".

Negation in Character Sets

You can negate a character set by using ^ as the first character after the opening bracket.

  • [^...]: Matches any single character not within the brackets.

    • 'ukraine' like '[^b]kraine' matches because the first character is not "b".

Case Sensitivity

The pattern matching is case insensitive.

  • 'poland' like 'POLAND' is a match despite differing case.

In

The in operator denotes a boolean value indicating whether the left hand side is contained in the right hand side.

return 1 in [1, 2, 3]; // true
return 0 in [1, 2, 3]; // false

let users = 
    {id: 0, name: "Cleo"},
    {id: 1, name: "Peter"}
];

return 1 in users.id; // true

Last updated