Skip to content

Latest commit

 

History

History
88 lines (81 loc) · 2.96 KB

protocol.md

File metadata and controls

88 lines (81 loc) · 2.96 KB

Protocol

A protocol is a Typescript interface that describes a behaviour.

For example:

export interface Value<T> {
    /**
     * `valueOf()` returns the data stored in this value.
     */
    valueOf(): T;
}

We've adoped the term protocol from the Swift community. In particular, we call the code that implements a protocol an extension.

We think about protocols in the same way that the Golang standard library uses interfaces.

// we write functions that accept protocols, not class names
//
// you can pass anything in as the `location`,
// as long as it implements the `Retrieval` protocol
function retrieve(logger: Logger, location: Retrieval) {
    logger.logInfo("retrieving data");
    return location.retrieve();
}