Skip to content

The Output data type

neverRare edited this page May 19, 2024 · 11 revisions

The src/output.md exposes Output class. Output is simply a wrapper for array data type. However, unlike array, Output also encodes error as a property. Error data types in JavaScript are often thrown but here, it is used as a typical data.

We need this to handle vague constructions of Toki Pona. Since many things in Toki Pona could mean many things.

class Output<T> {
  output: Array<T>;
  error: Array<OutputError>;

  // ...
}

An output is considered to have an error when the array is empty (Empty outputs are also used for initialization). The error property must be supplied if and only if the array is empty. Although this isn't a strict requirement.

Output is typically flat

TODO

Special Output types

  • ParserOutput
  • TranslationOutput

TODO more explanation.

Methods

The Output class only exposes 4 methods: constructor, isError, map, flatMap, and concat. constructor is used for constructing outputs (duh). isError is used for checking whether it is an error (also duh).

map and flatMap

With the virtue of functional programming, these are the only three methods used for manipulating outputs. The other one is concat which will be explained later.

class Output<T> {
    // ...

    map<U>(mapper: (value: T) => U): Output<U> { ... }
    flatMap<U>(mapper: (value: T) => Output<U>): Output<U> { ... }
}

These two methods are very similar to the array methods of the same name.

TODO

concat

TODO

Special OutputError type

Since the map method can accept thrown errors, it might accept errors not meant for Output. To remedy this, Output have its own Error type OutputError. If map have received OutputError, it will be properly handled, otherwise, it will be ignored and rethrown.

Clone this wiki locally