Menu

Classes that define their own interface

Mark Anthony Taylor (Shyreman)

Many interfaces only need one non-trivial implementation, and so become almost synonymous with the
class used to implement them. In these cases it can make sense to use the class to define the interface
itself.

Syntax:

(class <class-name>
    (defines <fully-qualified-interface-name>)
    ...
)

Example:

(class Dog
    (defines Sys.Animcals.IDog)
    (Int32 age)
    (Int32 breed)
)

(method Dog.Age -> (Int32 age):
    (age = this.age)
)

Every method that is defined for Dog becomes a method of the Sys.Animals.IDog interface, which
is defined and thus maintained by the Dog class.

One can also define an extension to an existent interface:

Syntax:

(class <class-name>
    (defines <base-interface-name> extends <fully-qualified-interface-name>)
    ...
)

Example:

(class Dog
    (defines Sys.IDog extends Sys.IAnimal)
    (Int32 age)
    (Int32 breed)
)

(method Dog.Age -> (Int32 age):
    (age = this.age)
)

Dog implements the existent interface IAnimal, but also defines and extends the interface to create
a Sys.IDog interface, the base of which is Sys.IAnimal.

~~~


Related

Wiki: Content