|
Spec-Zone .ru
спецификации, руководства, описания, API
|
Access a member (variable or function) of a class instance.
This example shows member access using a dot of both a variable (aPair.good) and a instance function (aPair.topsy()):
class Pair {
var good : String;
var bad : String;
function topsy() {
def tmp = good;
good = bad;
bad = tmp;
}
}
def aPair = Pair {
good: 'Sunflower Sprouts'
bad: 'Lard'
}
println( aPair.good );
aPair.topsy();
println( aPair.good );
It will print:
Sunflower Sprouts Lard
Function invocation, also known as function call, is how functions are, well..., called.
This is an example of calling a script function, see the Member Access section for an example of calling an instance function:
function squared(x : Number) : Number { x * x }
println( squared(14,2) );
A sequence select expression evaluates to a new sequence containing selected elements of another sequence.
Elements are included in the resulting sequence if the Boolean valueExpression evaluates to true. The name provides a name for each element for use in the valueExpression. For example:
def seq = [1..100]; def selected = seq[x | (x*x) < 20]; println( selected );
The selected elements of seq are those whose square is less than 20. So the following is printed:
[ 1, 2, 3, 4 ]
Note that this selection could also have been done with the equivalent for-expression:
def selected = for (x in seq where (x*x) < 20) x;
A sequence indexing expression accesses a single element of a sequence at the supplied index. If no element exists at that index, the sequence indexing expression will evaluate to the default value for the element specifier of the sequence.
For example:
def seq = [100..105]; println( seq[0] ); println( seq[3] ); println( seq[22] ); println( seq[-1] );
Here the element specifier of seq is Integer, so the following is printed -- since zero is the default value of Integer:
100 103 0 0
The type of a sequence indexing expression is that of the indexed sequence's element specifier (the element type).
A sequence slice expression evaluates to a sequence which is a portion of another sequence. The elements which make up the portion are specified by a range of indices.
The two valueExpressions give the beginning and ending of the range. The beginning is always inclusive, the ending of the range is inclusive unless the less-than sign is used. The ending valueExpression is optional, and if absent, the ending of the range is the last index of the sequence. For example:
def usprez = ['Washington', 'Adams', 'Jefferson', 'Madison', 'Monroe']; println( usprez[1..3] ); println( usprez[1..<3] ); println( usprez[3..] ); println( usprez[3..<] );
Will print to the console:
[ Adams, Jefferson, Madison ] [ Adams, Jefferson ] [ Madison, Monroe ] [ Madison ]
The type of a sequence slice expression is the type of the sequence to which the slice clause is applied.