Class concept (SDL script)
SDL Script Extensions for TDK:
%class MyClass
%object AnObject
%numeric SomeMember
%list AList
%string AString
MyClass::AnOperation (2)
%static MyClass::AStaticOperation (1)
%endclass
%operation MyClass.AnOperation (%object O, %string X) %return %string
%return AString & O.Name & X
%endoperation
%static %operation MyClass.AStaticOperation (%object O) %return %string
%return O.Name
%endoperation
It is possible to define classes in TDK sdl. Class behaviour is essentially the same as most OO languages.
Classes can have attributes and operations. Classes can be inherited such that new attributes and operations can be added to the derived class and behaviour of operations already existing in the parent class can be redefined.
The declaration of a class in sdl is fully model-driven by means of the TDK metagen code generator. This generator will translate the generator model classes belonging to the generator model to a set of .sdl files, one per class. Given the absence of class specification files (no equivalent of Cpp.h in sdl), the metagen copies the declaration of parent class attribute members to the derived classes.
Typical layout of an .sdl class file:
%class MyClass
...class members here...
%endclass
%operation NewMyClass () %return %object
...constructor code, automatically generated by metagen...
%endoperation
%operation MyClass.SomeOperation ()
...
%endoperation
%static %operation MyClass.SomeStaticOperation ()
...
%endoperation
etc.
Static vs non-static operations and Self implicit parameter
Operations on classes can be static or non-static:
• Non-static operations have one extra first parameter that is automatically passed when the operation is called. This parameter can be referenced from within the operation body (when non-static). The name of this parameter is Self (case sensitive).
• Static operations don't have the Self parameter.
Within a class non-static operation body the syntax
% string A
A = B
is automatically translated to
A = Self.B
if B has been declared as a class attribute in the class specification section.
Similarly,
A = B ( )
is translated to
A = Self.B ( )
if B is an operation defined in the subject class.
Operation resolution concepts
Operation calls (that is the link between called and called) are resolved once at the time the generator is loaded. Two operations with the same name are different if the following conditions apply:
• They are defined on different classes.
• They are both defined in the same class or they are both defined outside any class, but they have a different number of parameters (parameter type does not matter).
• One of the operations is defined on a class, the other is not.
An operation is a 'free' operation, that is it is not defined in any class, when its declaration does not start with the class name. For example, %operation AFreeOperation () is free.
Note that the New... operation, automatically generated by metagen, is a free operation.
Operation call examples
%object A
A = NewMyClass ()
A.SomeOperation ()
MyClass::SomeStaticOperation () $ Self is not passed and no object needed
CallOfAFreeOperation () $ No object for a free operation
$ provided GetMessage is defined in MyClass and returns a %string
%message A.GetMessage ()