Skip to main content
Version: Next

Interfaces

An interface is a list properties that you can apply to a class. When you apply an interface to a class, that class must have all of the properties declared in the interface. The class can also have additional definitions that are not in the interface. The LIGO compiler warns you of any mismatches between the interface and the class.

For example, the following interface specifies that a class must have these contents:

  • A property named add, which accepts two values of type nat and returns a value of type nat
  • Values one and two, which are of the type nat
interface Euro_INTF {
add: (a: nat, b: nat) => nat;
one: nat;
two: nat;
};

To apply an interface to a class, put the name of the interface after the keyword implements and the class name, as in the following example. We say that the class implements the interface. This class defines the properties add, one and two. It also adds a property named multiply that is not specified in the interface:

class Euro implements Euro_INTF {
static add = (a: nat, b: nat): nat => a + b;
static one = 1 as nat;
static two : nat = 2 as nat;
multiply = (a: nat, b: nat): nat => a * b;
};

Note how properties from the interface must be defined as static in the class.

Extending interfaces

Interfaces can be extended by inheritance with the extends keyword, as in this example:

interface Euro_INTF {
add: (a: nat, b: nat) => nat;
one: nat;
two: nat;
};
interface WithTenEuro_INTF extends Euro_INTF {
ten: nat;
};
interface WithFiftyEuro_INTF extends Euro_INTF {
fifty: nat;
};

Interfaces can extend more than one interface, which can lead to an interface that extends a base interface more than once, known as diamond inheritance. For example, the following interface extends two interfaces from the previous example. Because both of these interfaces extend the same base interface, it is as if the interface extends the base interface twice. Diamond inheritance doesn't cause any problems for the interface.

interface NewEuro_INTF
extends WithTenEuro_INTF, WithFiftyEuro_INTF {
hundred: nat;
five_hundred?: nat;
};

Interfaces can have optional types and values indicated with a question mark ?. In the previous example, the interface NewEuro_INTF has an optional property five_hundred. This class defines this optional value and adds a property named twenty that is not defined in the NewEuro_INTF interface:

class NewEuro implements NewEuro_INTF {
static add = (a: nat, b: nat) => a + b;
static one: nat = 1;
static two: nat = 2;
static ten: nat = 10;
static fifty: nat = 50;
static hundred: nat = 100;
static five_hundred: nat = 500; // Could be omitted
static twenty: nat = 20; // Extra new constant
}