Skip to content

Latest commit

 

History

History
102 lines (73 loc) · 2.08 KB

interfaces.md

File metadata and controls

102 lines (73 loc) · 2.08 KB
id title
interfaces
Interfaces

Interfaces

Interfaces can be used to group and reuse operations.

Interfaces are declared using the interface keyword.

interface SampleInterface {
  foo(): int32;
  bar(): string;
}

Composing interfaces

The keyword extends can be used to compose operations from other interfaces into a new interface.

Given the following interfaces

interface A {
  a(): string;
}

interface B {
  b(): string;
}

a new interface C can be created including all operations from A and B

interface C extends A, B {
  c(): string;
}

which is equivalent to

interface C {
  a(): string;
  b(): string;
  c(): string;
}

Interface template

Interfaces can be templated, see templates for details on templates.

interface ReadWrite<T> {
  read(): T;
  write(t: T): void;
}

Interface operation templates

Operations defined inside of an interface can also be templated. (see templates for details on templates.)

interface ReadWrite<T> {
  read(): T;
  write<R>(t: T): R;
}

alias MyReadWrite = ReadWrite<string>;

op myWrite is MyReadWrite.write<int32>;

:::caution Any uninstantiated, templated operation defined in an interface will be excluded from the list of service operations.

This also applies when using extends on an interface that contains templated operations with unfilled template arguments.

interface ReadWrite<T> {
  read(): T;
  write<R>(t: T): R;
}

interface MyReadWrite extends ReadWrite<string> {} // Here the `read()` operation is fully instantiated and will be included in a service definition. `write()` however isn't.

When working with building block interface like this use alias to create your interface building block instead of interface extends. This way the instantiated interface and its member will not be resolved in the service definition.

alias MyReadWrite = ReadWrite<string>;

op myRead is MyReadWrite.read;
op myWrite is MyReadWrite.write<int32>;

:::