Previous section To contents Next section

5.7 The rest of the operators

Now there are only a couple of operators left. I have grouped them together in this section, not because they are not important, but because they do not fit in any particular categories.

Function Syntax Identifier Returns
       
splice @ a none Sends each element in the array a as an individual argument to a function call.
Increment ++ a none Increments a and returns the new value of a.
Decrement -- a none Decrements a and returns the new value of a.
Post increment a ++ none Increments a and returns the old value of a.
Post decrement a -- none Decrements a and returns the old value of a.
casting (type) a none Tries to convert a into a value of the specified type.
Null a, b none Evaluates a and b, then returns b.

The most important of these operators is the calling operator. It is used to call functions. The operator itself is just a set of parenthesis placed after the expression that returns the function. Any arguments to the function should be placed between the parenthesis, separated by commas. We have already seen many examples of this operator, although you might not have realized it was an operator at the time. The function call operator can do more than just calling functions though; if the 'function' is in fact an array, the operator will loop over the array and call each element in the array and returns an array with the results. If on the other hand, the 'function' is a program, the operator will clone an object from the program and call create() in the new object with the arguments given. In fact, the function clone is implemented like this:

object clone(mixed p, mixed ... args) { ( (program)p )(@args); }

On the subject of function calls, the splice operator should also be mentioned. The splice operator is an at sign in front of an expression. The expression should always be an array. The splice operator sends each of the elements in the array as a separate argument to the function call. The splice operator can only be used in an argument list for a function call.

Then there are the increment and decrement operators. The increment and decrement operators are somewhat limited: they can only be used on integers. They provide a short and fast way to add or subtract one to an integer. If the operator is written before the variable (++a) the returned value will be what the variable is after the operator has added/subtracted one to it. If the operator is after the variable (a++) it will instead return the value of the variable before it was incremented/decremented.

Casting is used to convert one type to another, not all casts are possible. Here is a list of all casts that actually _do_ anything:

casting from to operation
int string Convert the int to ASCII representation
float string Convert the float to ASCII representation
string int Convert decimal, octal or hexadecimal number to an int. Note that this will only work with decimal numbers in future versions.
string float Convert ASCII number to a float.
string program String is a filename, compile the file and return the program. Results are cached.
string object This first casts the string to a program, (see above) and then clones the result. Results are cached.
object type This calls the function 'cast' with a string containing the type as an argument.
string array Same as doing values(string)
array(int) string This does the inverse of the operation above. Ie. it constructs a string from an array of integers.
array array(type) This recursively casts all values in the array to type.
mapping array Same as Array.transpose(({indices(mapping),values(mapping)). Example: (array)([1:2,3:4]) will return ({ ({1,2}), ({3,4}) })
multiset array Same as doing indices(multiset).
int float Returns a float with the same value as the integer.
float int Returns the integer closest to the float.
function object Same as function_object(function).

You can also use the cast operator to tell the compiler things. If a is a variable of type mixed containing an int, then the expression (int)a can be used instead of a and that will tell the compiler that the type of that expression is int.

Last, and in some respect least, is the comma operator. It doesn't do much. In fact, it simply evaluates the two arguments and then returns the right hand one. This operator is mostly useful to produce smaller code, or to make defines that can be used in expressions.


Previous section To contents Next section