Semicolon in array definition

I spent some time today tracking down a bug in my code that essentially boiled down to the following:

a = [1; 2, 3];

Not only is this a valid expression in sclang, but after running it, the value of a is:

-> [ 2, 3 ]

Is this by design? I cannot think of a situation where this would be useful but maybe I am missing something. Shouldn’t this produce an error?

a = [1, 2; 3] // -> [ 1, 3 ]

Given the above example, is sclang allowing multi-line expressions “between” the commas, so that the second item in the array above is actually:

2;
3

which is evaluated as 3. Is that what is going on?

Yep.

http://doc.sccode.org/Reference/Expression-Sequence.html

Functions aren’t made of statements separated by semicolons.

A function contains an expression, which may be a single expression or a sequence of them (separated by semicolons).

“Such a sequence may be used anywhere that a normal expression may be used” (from the help file) – including expressions in arrays.

hjh

Well, you learn something new every day, thanks for the pointer to the help file.

b = 0;
a = [1, b = 1; 3];
a; // -> [ 1, 3 ]
b; // -> 1

Wild stuff!

In the SC4 speculation thread, there are understandably a lot of gripes about the old-fashioned or weaker elements in sclang’s design, but it does have a kind of purity about it that I find appealing.

; isn’t a statement terminator as in C because SC has no statements, only var/arg declaration blocks and expressions. If you’re used to C style languages, you’ll think, “Yeah but they’re really statements,” but they’re not – if it’s not declaring a var or arg, then it’s an expression, period. It has a value. a = stream.next is an expression whose return value is the right hand side and whose side effect is to update a. It is not an “assignment statement.” So you can do, for instance,

f = { arg stream;
    var ch;
    while {
        (ch = stream.next).notNil and: { ch != $\n }
    } {
        ... loop body...
    };
};

… because the expression ch = stream.next can appear anywhere an expression is valid.

I’ve gotten away from this style, for clarity, but it’s legal.

(Similarly, SC doesn’t have control structures. If, while, case, switch, do etc are all methods. Nor does it have any operators – binary ops in infix notation compile to method calls – so there’s no special handling for operator overloading.)

hjh

1 Like