Anonymous user
Category:Elena: Difference between revisions
no edit summary
No edit summary |
|||
Line 57:
Several parameters can be passed in the message as well:
control foreach: (1,2,3)
The generic message can have several parameters as well:
Line 89 ⟶ 87:
class DerivedClass :: BaseClass
{
constructor new
[
theField1 := aField1.
Line 95 ⟶ 93:
]
add
= MyClass new
}
To create a class instance we have to send a message (usually new) to its symbol (a class symbol is declared implicitly for every class and can be used as a normal one)
var anObject := DerivedClass new
Singletons cannot have constructors and their symbols can be used directly
Line 108 ⟶ 106:
{
sumOf:a:b
= a add
}
Line 117 ⟶ 115:
In general the symbol is a named expression and can be used to declare initialized objects, constants, reusable expressions and so on.
symbol ZeroClass = DerivedClass new
A static symbol is the class instance which state is preserved. There could be only one instance of static symbol.
static SingletonClass = DerivedClass new
== Code blocks ==
Line 127 ⟶ 125:
ELENA code block consists of a sequence of statements. The block is enclosed in square brackets and may contain nested sub code blocks (which in fact are inline action classes). The statement terminator is a dot.
printAckermann
[
control forrange
[
control forrange
[
...
console writeLine
].
].
Line 145 ⟶ 143:
...
^ aRetVal / anArray length
]
Line 151 ⟶ 149:
Number = convertor toReal:theValue.
It is possible to declare the block variable and assigns the value to it. The variable name must be unique within the code block scope.
Line 196 ⟶ 188:
(m == 0)
? [ ^n + 1 ]
! [ ^m + n ].
Note: the main difference between using explicit messages and conditional operators is that the compiler may optimize the resulting code in the later case.
Line 204 ⟶ 196:
(m == 0)
! [ ^m / n ].
It is possible to use *if* template code :
Line 218 ⟶ 210:
if (aChar >= 48) and:(aChar < 58)
[
theToken
];
[
Exception new:"Invalid expression"; raise.
]
Note that in this case both condition will be evaluated even if the first one is false If we want to use short-circuit evaluation expression brackets should be used
if ((x >= 0)and:
[
...
Line 232 ⟶ 224:
A switch statement can be implemented using => operator
-1 [ consoleEx writeLine:"Not a valid guess.". ^ true
4 [
consoleEx writeLine:"Congratulations! You have won!".
^ false
];
! [
theAttempt
consoleEx writeLine:
Line 245 ⟶ 237:
: " bulls and " : aCows : " cows".
^ true
].
|