-
Notifications
You must be signed in to change notification settings - Fork 0
The Output data type
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.
TODO
ParserOutput
TranslationOutput
TODO more explanation.
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).
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
TODO
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.