Friday, March 10, 2017

ELENA 3.0 : event handler

In this short article I will show how to implement event handler using a new attribute : event

To declare a new event handler we should declare the field with event attribute:

func1 event onStart :: theStart.

The first parameter indicates the action type (func1 is an action with a single generic parameter), the second one - the property name to be used for attaching the handler.

To attach the handler we have to call the property with our action:

    object onStart func1(:x)[ /*...*/ ].

The simple example code is below:

import extensions.

class MyClass
{
    func1 event onStart :: theStart.

    func1 event onEnd   :: theEnd.
        
    start : o
    [
        ($nil != theStart)
            ? [ theStart eval:o ].        
    ]
        
    stop : o
    [
        ($nil != theEnd)
            ? [ theEnd eval:o ].        
    ]
}
      
symbol program =
[
    var o := MyClass new.
    
    o onStart func1(:x) [ console printLine("a:",x) ].
    o onStart func1(:x) [ console printLine("b:",x) ].

    o start:1.
].

No comments:

Post a Comment