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) &do:printingLn.
 
Ampersand is used to indicate that the signature has several arguments (subjects). The actual message name is eval&foreach&do(2).
 
The generic message can have several parameters as well:
Line 89 ⟶ 87:
class DerivedClass :: BaseClass
{
constructor new &field1:aField2 &field2:aField2
[
theField1 := aField1.
Line 95 ⟶ 93:
]
add &field1:aField2 &field2:aField2
= MyClass new &Field1:(theField1 + aField1) &Field2:(theField2 + aField2).
}
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 &field1:1 &field2:1. // DerivedClass is a symbol
 
Singletons cannot have constructors and their symbols can be used directly
Line 108 ⟶ 106:
{
sumOf:a:b
= a add &field1:b &field2:a.
}
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 &field:0 &field:0.
 
A static symbol is the class instance which state is preserved. There could be only one instance of static symbol.
 
static SingletonClass = DerivedClass new &field:0 &field:0.
 
== 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 &n:n &m:m
[
control forrange &int:0 &int:n &do: (&int:i)
[
control forrange &int:0 &int:m &do: (&int:j)
[
...
console writeLine.
].
].
Line 145 ⟶ 143:
...
^ aRetVal / anArray length.
]
 
Line 151 ⟶ 149:
 
Number = convertor toReal:theValue.
 
or there is an alternative block expression
 
[ convertor toReal:theValue ]
 
Note: it should not end with the terminator symbol
 
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 += append:aChar.
];
[
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:[ $(array@x != 0]))
[
...
Line 232 ⟶ 224:
A switch statement can be implemented using => operator
 
^ aBulls =>
-1 [ consoleEx writeLine:"Not a valid guess.". ^ true. ];
4 [
consoleEx writeLine:"Congratulations! You have won!".
^ false.
];
! [
theAttempt += append:1.
consoleEx writeLine:
Line 245 ⟶ 237:
: " bulls and " : aCows : " cows".
^ true.
].
Anonymous user