Category:Elena: Difference between revisions

no edit summary
No edit summary
Line 77:
ELENA does not support multiple inheritance, though it is possible to inherit the code using redirect handler (so called "horizontal inheritance"). When the parent is not provided the class inherits directly system'Object (the super class).
 
#class BaseClass
{
#fieldobject theField1.
#fieldobject theField2.
#method field1 = theField1.
#method field2 = theField.
#method field2 = theField.
}
#class DerivedClass :: BaseClass
{
#constructor new &field1:aField2 &field2:aField2
[
theField1 := aField1.
Line 96 ⟶ 95:
]
#method add &field1:aField2 &field2:aField2
= MyClass new &Field1:(theField1 + aField1) &Field2:(theField2 + aField2).
}
Line 102 ⟶ 101:
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
 
RolesSingletons cannot have constructors and their symbols can be used directly
 
#class(role) ClassHelper =
{
#method sumOf:anObject1a:anObject2b
= anObject1a add &field1::anObject2b &field2::anObject1a.
}
...
#var aSum := ClassHelper sumOf:anObject11:anObject22.
 
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 128 ⟶ 127:
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.
 
#method printAckermann &n:n &m:m
[
control forrange &int:0 &int:n &do: (&int:i)
Line 151 ⟶ 150:
If the code block contains only return statement the simplified syntax can be used:
 
#method Number = convertor toReal:theValue.
 
or there is an alternative block expression
Line 161 ⟶ 160:
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.
 
#var aRetVal := Integer new:0.
 
 
Line 168 ⟶ 167:
ELENA like Smalltalk does not support any special language constructs to implement the conditional branching. Instead special Boolean symbols (system’true and system’false) are used. All conditional operations should return these symbols as a result.
 
There are three branching methods : thenif[1] , then&elseif[2], else[1]
 
(m == 0) thenif:
[
n + 1
]
&else: [
m + n
].
Line 180 ⟶ 179:
Note that code in square brackets are in fact nested action classes ( an action class is a class supporting evaluate message). So this code is can be written in this form:
 
(m == 0) thenif:
{
eval
Line 187 ⟶ 186:
]
}
&else: {
{
eval
[
Line 215 ⟶ 213:
]
! [
#throw Exception new:"Invalid expression" raise.
]
 
Anonymous user