Spec-Zone .ru
спецификации, руководства, описания, API

valueExpression

Figure 6.13. valueExpression

valueExpression

ifExpression

The ifExpression is used to selectively evaluate code.

Figure 6.14. ifExpression

ifExpression

If the else part is present and neither of the expressions is of Void type, then the ifExpression has a value. That value is the value of the first expression if valueExpression is true, and otherwise the value of the second expression. The type of such an ifExpression is the most specific type that has the type of the first expression as a subtype and also has the type of the second expression as a subtype. For example:

var x = if (bip) "Fern" else 2.68;

Here, bip must be a Boolean variable. If bip is true the value of x is "Fern". If bip is false the value of x is 2.68. The type of x is Object as there is no more specific type that has String and Number as subtypes.

If there is no else part or either expression is of Void type, then the ifExpression has Void type. For example:

if (temp > 1000) {
   println("Reactor breach!")
}

Here the println will be executed if the temp is greater than 1000.

forExpression

The forExpression iterates over one or more sequences. The expression is called the body of the forExpression. If the body is of Void type, then the forExpression has Void. Otherwise the value of the forExpression is a sequence consisting of each body value.

Figure 6.15. forExpression

forExpression

Figure 6.16. inClause

inClause

This example shows a simple for-expression whose value is a sequence:

var squares : Integer[] = for (x in [1..10]) x*x;

The values of squares is [ 1, 4, 9, 16, 25, 36, 49, 64, 81, 100 ]. Note that the type of squares is explicitly declared for illustrative purposes, type inference would correctly determine that the type is Integer[].

This for-expression is of Void type. It is used simply to initerate over the block:

for (counter in [1..3]) {
   println( "Help!" );
}

If there are multiple inClauses, the right-most inClause is the inner-most loop, the body is only evaluated for cases where the where-clause is true. Thus:

var words = for (length in [3..6], word in ['moose', 'wolf', 'turkey', 'bee'] where word.length() >= length) word;

Thus the value of words is:

[ moose, wolf, turkey, bee, moose, wolf, turkey, moose, turkey, turkey ]

Because of sequence flattenning and because null is never added to a sequence, this is equivalent to:

var words = for (length in [3..6]) for (word in ['moose', 'wolf', 'turkey', 'bee']) if (word.length() >= length) word else null;

newExpression

A newExpression can be used to create an instance of a Java class. It allows the arguments to a Java constructor to be provided. It can also be used to create an instance of a JavaFX class, but objectLiteral is generally used for that purpose.

Figure 6.17. newExpression

newExpression

For example:

var stream = new java.io.FileInputStream("myBuddies.gif");