Interfaces
An interface is a list of types and values that you can apply to a namespace. When you apply an interface to a namespace, that namespace must have all of the types and values in the interface. The namespace can also have additional definitions that are not in the interface. The LIGO compiler warns you of any mismatches between the interface and the namespace.
For example, the following interface specifies that a namespace must have these contents:
- A type named
t
, although the data type of that type is not specified, which makes it an abstract type - A function named
add
, which accepts two values of typet
and returns a value of typet
- Values
one
andtwo
, which are of the typet
To apply an interface to a namespace, put the name of the interface after the keyword implements
and the namespace name, as in the following example.
It's said that the namespace implements the interface.
This namespace defines the type t
as a nat and defines the add
function and the one
and two
values.
It also adds a function named multiply
that is not specified in the interface:
The namespace must instantiate any abstract type in the interface, as this namespace defines the abstract type t
as a nat.
Extending interfaces
Interfaces can be extended by inheritance with the extends
keyword, as in this example:
Note that the type t
remains abstract in all of the interfaces.
Namespaces that use any of these interfaces must instantiate the type.
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.
Interfaces can have optional types and values indicated with a question mark ?
.
In the previous example, the interface NewEuro_INTF
has an optional value five_hundred
.
This namespace defines this optional value and adds a value named twenty
that is not defined in the NewEuro_INTF
interface: