The Freya Programming Language

Interface delegations

See also

An interface delegation is a special member kind provided by Freya for reusing already existing interface implementations.

Syntax

Interface delegations are just an implementation detail, and they are not part of the public contract of the declaring type. So, interface delegations belong to the type's implementation section. There are two kinds of interface delegations. The first kind delegates an interface implementation to the result of an expression compatible with the interface type. Most of the times, the expression will make reference to a field or a property:

interface type-reference is expression;

The other kind of interface delegation changes the is keyword by an equality:

interface type-reference = expression;

In this case, the compiler creates a hidden field to store the result of evaluating the expression. Then, this hidden field is handled as in the former delegation variant.

Examples

Suppose we want to implement a given interface type, such as IEnumerable[X], for a class:

MyClass = class(IEnumerable[Integer]);

Since the generic List[X] class already implements IEnumerable[X], we can declare a private list field inside the class in order to use it in an interface delegation:

implementation for MyClass is

    // A private field
    var internalList: List[Integer];

    interface IEnumerable[Integer] is internalList;

We're assuming you'll initialize the internal list inside the constructor. However, we may choose to delegate the interface implementation in this other way:

implementation for MyClass is

    interface IEnumerable[Integer] = new List[Integer]![1, 2, 3, 4];

We have used a collection initializer to populate the list. We could also have used an integer array, since arrays always implements IEnumerable[X]:

implementation for MyClass is

    interface IEnumerable[Integer] = [1, 2, 3, 4];

See also

The Freya Programming Language
Type declarations
Interface declarations
Type members
Statements
Expressions