S2:Operator

There are many operators in S2.

The following text is taken from Section 4 of the existing S2 Documentation.

= Operators = S2 operators have the following associativity and precedence, listed from highest precedence to lowest.

Arithmetic Operators
The S2 language has the usual set of arithmetic operators you'd expect to find in a programming language. These are the same as the arithmetic operators in Perl.

As you might expect, these operators can only be applied to integers, and will return integers.

Comparison Operators
These operators allow you to compare one value to another. Both operands must be of the same type, and all but simple equality/inequality can only be applied to integers. Only primitive types may be compared with these operators. All comparison operators return a boolean value.

Logical Operators
These operators perform logical operations on boolean values. All operands must be boolean, and a boolean value results.

Other Operators
These operators don't really fit into a category of their own, and have perhaps non-obvious functions.

Assignment Operator: =
The assignment operator (which should not be confused with the equality test operator, ), is used to assign a value to a named variable. This means that unlike most other operators, its left operand must be a variable name. No other kind of expression will do.

The assignment operator can take operands of any type, but the right-hand operand must be of the same type as the variable given on the left, or coercable into that type.

The assignment operator also returns whatever value it has just assigned to the variable, meaning that you can chain several assignments together as follows:

$foo = $bar = $moo = 2;

In this case,,   and   will all be assigned the value.

As a special case, assignment can also be used when declaring local variables:

var string name = "John";

However, this is only true for local variables. Member variables cannot be initialized in this way.

The symbol  is used for several other assignment-like things in the S2 language, including setting property values and layerinfo. These aren't really assignment, but act similarly and in the case of property setting follow many of the rules described above.

Concatenation Operator: +
The  symbol actually has two purposes in the S2 language. When one of its operands is a string, it becomes the string concatenation operator. In this form, it will firstly attempt to coerce any non-string operand into a string, then stick the two strings together to form a resulting string which is returned.

The simplest form of string concatenation involves two strings. In this case, both strings are just concatenated and that is it. However, as long as one operand remains a string, the other operand can be an integer, a boolean or an object with certain conditions. Objects may only be concatenated to strings if their class has an  method which returns a string. The return value of this method will be used as the string value for concatenation.

String literals with interpolated variables are really just concatenation with a more convenient syntax, which is why you are able to interpolate integers and certain objects directly into string literals.

Ternary Conditional Operator: ? :
This is the only operator in the S2 language with three operands. Its function can be thought of as being like an  statement in the form of an operator.

This operator is best explained with an example:

var string label = ($c == 1 ? "1 comment" : "$c comments");

In this example, the conditional operator and its operands are enclosed in parenthesis for clarity. These are optional, but can make things more readable. The first operand, preceding the question-mark symbol, is a boolean expression. In this case it is an equality test. If this expression evaluates to true, the second operand, which is after the question-mark and before the colon, is returned. If the expression is false, the third operand is returned.

The first operand must be a boolean expression. The second and third operands can be of any type but their types must match. The return type of this operator is the same as that of the second and third operand. In the above example, then, the return type would be string.

In all cases an  construct can be used in place of this operator, but the conditional operator is shorter and more readable in some cases.

Range Operator: ..
This rather quirky operator is borrowed from Perl. It takes two integer operands and returns an array of integers containing all integers between the first and second operand, inclusive. For example:

var int[] list = 1 .. 10;

The array  will now contain ten elements, each numbered in order from   to. This has the same effect as:

var int[] list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

This operator is really only useful for creating a loop which will iterate a certain number of times:

foreach var int i (1 .. 10) { println "Iteration number $i"; }

This example will print ten lines, each containing a number from  to   in order.

Named Unary Operators
The following are the built-in named operators. You do not have to wrap the following term in parentheses.


 * isnull $object
 * returns a boolean: true if $object is null (has no value)


 * defined $object
 * opposite of isnull. might be prettier than negating isnull


 * new ClassName
 * returns an instance of ClassName with all members empty or zero


 * newnull ClassName
 * returns an undefined value of type ClassName


 * reverse $string
 * returns copy of $string with characters reversed


 * reverse $array
 * returns shallow copy of $array with elements reversed.


 * size $array
 * returns number of elements in $array