Expressions

Sather - A Language Tutorial
Prev | Chapter 15. Statement and Expression Catalogue |

# 15.2. Expressions

We describe below a few special expressions used in Sather - `void`, `void()` and the short circuit boolean operations `or` and `and`.

## 15.2.1. `void` expressions

A void expression returns a value whose type is determined from context. `void` is the value that a variable of the type receives when it is declared but not explicitly initialized. The value of `void` for objects (except for immutable objects) is a special value that indicates the absence of an object - it is essentially the NULL pointer. Immutable objects are described in their own chapter, but for the sake of reference:

    Class     Initial Value
    -----------------------
    INT       0
    CHAR      '\0'
    FLT       0.0
    FLTD      0.0d
    BOOL      false

For other immutable types the void value is determined by recursively setting each attribute and array element to `void`[1]. For numerical types, this results in the appropriate version of 'zero'.

> [1] The other built-in basic types are defined as arrays of BOOL and all have their values set to void by this rule.

`void` expressions may appear

- as the initializer for a constant or shared attribute. In fact, for most built-in classes, the only legal constant value is the void value e.g.

    const a: POINT := void;

- as the right hand side of an assignment statement

- as the return value in a `return` or `yield` statement

- as the value of one of the expressions in a `case` statement

- as the exception object in a `raise` statement (see the chapter on Exceptions)

- as an argument value in a method call

- in a creation expression. In this last case, the argument is ignored in resolving overloading.

`void` expressions may not appear:

- as the left argument of the dot '`.`' operator.

      a: POINT := #POINT(3,3); -- ILLEGAL (and silly) a.void

It is a fatal error to access object attributes of a void variable of reference type or to make any calls on a void variable of abstract type. Calls on a void variable of an immutable type are, however, quite legal (otherwise you would not be able to dot into a false boolean or a zero valued integer!)

## 15.2.2. `void` test expressions

Example:

    void(x)

Void test expressions evaluate their argument and return a boolean value which is true if the value is `void` .

    p: POINT;
    #OUT + void(p);   -- Prints out true
    p := #POINT(3,5);
    #OUT + void(p);   -- Prints out false
    p := void;
    #OUT + void(p);   -- Prints out true;
    b: BOOL;
    #OUT + void(b);   -- Prints out true
    b := false;
    #OUT + void(b);          -- Prints out true!
    -- Even though b has been assigned, it has the void value

## 15.2.3. Short circuit boolean expressions: `and` and `or`

Example:

    if (3>a and b>6) or (c = "Goo") then
       #OUT + "Success!"
    end;

and expressions compute the conjunction of two boolean expressions and return boolean values. The first expression is evaluated and if `false`, `false` is immediately returned as the result. Otherwise, the second expression is evaluated and its value returned.

or expressions compute the disjunction of two boolean expressions and return boolean values. The first expression is evaluated and if `true`, `true` is immediately returned as the result. Otherwise, the second expression is evaluated and its value returned.

For an expression description see unnamedlink

## 15.2.4. `exception` expressions

Example:

    protect
       ... some code
    when STR then
       #OUT + exception.str;
    when ...
    else ...
    end;

exception expressions may only appear within the statements of the `when` and `else` clauses in `protect` statements. They return the exception object that caused the `when` branch to be taken in the most tightly enclosing `protect` statement. The return type is the type specified in the corresponding `when` clause (See unnamedlink). In an `else` clause the return type is '`$OB`'.

Prev | Home |
Statement and Expression Catalogue | Up |