Basics

Guides

API Reference

Menu

Basics

Guides

API Reference

Operators

This page lists every operator in Aussom and shows how to use it.

Comparison

a == b      // equal
a != b      // not equal
a >  b      // greater than
a <  b      // less than
a >= b      // greater than or equal
a <= b      // less than or equal

Logical

!ok         // not
a && b      // and
a || b      // or

&& and || short-circuit. The right side is not evaluated when the left is enough to decide the result.

if (user != null && user.active) {
    // user.active is read only if user is not null
}

Arithmetic

x = 5 + 10;     // 15  - addition
x = 10 - 5;     // 5   - subtraction
x = 10 * 5;     // 50  - multiplication
x = 10 / 5;     // 2   - division

Integer arithmetic stays integer until a double is involved. Once any side is a double, the result is a double.

a = 7 / 2;      // 3   (integer division)
b = 7.0 / 2;    // 3.5 (one side is double)

Compound assignment shortcuts:

x += 5;         // x = x + 5
x -= 5;         // x = x - 5
x *= 5;         // x = x * 5
x /= 5;         // x = x / 5
x++;            // x = x + 1
x--;            // x = x - 1

String concatenation

The + operator joins strings. When one side is a non-string, it gets converted automatically.

name = "Avery";
msg = "Hi " + name;          // "Hi Avery"
note = "Age: " + 32;         // "Age: 32"
mix = "Sum is " + (20 + 12); // "Sum is 32" - parens evaluate first

Without parentheses, + is left-associative, so "Sum is " + 20 + 12 becomes "Sum is 2012" because the first + produces a string and that string is then concatenated with 12.

Collection operators

items = [];
items @= "first";       // append - list now ["first"]
items @= "second";      // list now ["first", "second"]

count = #items;         // 2 - count operator works on list and map

first = items[0];       // index access (0-based)
items[1] = "changed";   // assign by index

m = { a: 1, b: 2 };
m["c"] = 3;             // add to map
val = m["a"];           // 1
n = #m;                 // 3

The # operator works on lists, maps, and strings (length in characters).

Initial values for class members

Maps and lists assigned in a class declaration must use only primitive values - you cannot call functions or do complex work in the default. Inside a function, you can use any expression you like.

class MyClass {
    // OK - primitive defaults
    public languages = ["c", "c++", "java", "aussom"];
    public config = {
        one: 1,
        two: 2,
        nested: { make: "soap" }
    };

    public someFn() {
        // OK - any expression is allowed inside a function
        m = { one: 1, two: this.getAge(), three: [1, 2, 3] };
        m["four"] = computeSomething();
    }

    public getAge() {
        return 42;
    }
}

Safe access with ?

The ? operator placed before an expression returns null instead of throwing when:

  • a member does not exist on an object,
  • a key does not exist in a map, or
  • an index is out of bounds on a list.
val = ?this.does.not.exist;
val = ?someVar.missing.field;
val = ?config["missing"].nested;
val = ?items[1000];

This is handy when mapping data from an external source where optional fields may simply be absent. It saves a chain of explicit checks.

? only catches member-not-found, key-not-found, and index-out-of-bounds. Other exceptions (like divide by zero) still propagate.

// Returns null - val.doesnt.exist throws member-not-found,
// which the ? catches.
ret = ?val.obj[val.doesnt.exist].name;

// Throws - the divide-by-zero exception is not one of the kinds
// that ? catches.
ret = ?val.obj[10/0].name;

Set block: :=

Bulk assigns members from a map. See the Functions page for details. Quick example:

u = new User() := {
    firstName: "Robert",
    lastName: "Hogan"
};

Callback: ::

Creates a reference to a function. Pass it around like a value and let some other code call it later.

cb = ::onClick;
button.onClick(cb);

See the Data Types page for the full description of callbacks.

Operator precedence

From highest to lowest:

  1. Member access (.), index access ([]), function call (()).
  2. Unary: !, unary -, prefix ?.
  3. *, /.
  4. +, -.
  5. Comparisons: <, <=, >, >=.
  6. Equality: ==, !=.
  7. Logical: &&, then ||.
  8. Assignment: =, +=, -=, *=, /=, @=, :=.

When in doubt, add parentheses. They never hurt clarity and they match the precedence Aussom uses.