The Freya Programming Language

Events

See also

An event stores a delegate chain, and it is used in a different way depending on you're using the event from inside or outside the declaring class. Inside the class, you can use the event as a field or property declared with a delegate type. Outside the class, you can only add and remove delegates from the event chain.

Event declarations

An event declaration includes a name for the event and its type, which must be a delegate type:

event identifier : delegate-type-reference

Though not mandatory, the delegate type must conform to the following pattern:

Event implementation

In most cases, the event declaration is not matched by an event implementation. In this case, the compiler synthesizes an implementation, using a hidden field to hold the delegate chain. Inside the class, you can use the event name as you would use a field.

However, you can obtain more control by providing an explicit implementation. An explicit event implementation requires two access methods:

MyEventType = method(Sender: Object; E: EventArgs);

MyClass = class
public
    event MyEvent: MyEventType;
    // ...
end;

implementation for MyClass is

    var myEventField: MyEventType;

    event MyEvent.Add(D: MyEventType);
    begin
        myEventField += D;
    end;

    event MyEvent.Remove(D: MyEventType);
    begin
        myEventField -= D;
    end;

As the previous example shows, you must provide two access methods, with the names eventname.Add and eventname.Remove. Both methods must accept a single parameter with the event type, and they cannot have a return type.

See also

The Freya Programming Language
Type declarations
Type members
Constructors
Methods
User defined operators
Iterators
Fields
Properties
Indexers
Statements