-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.ts
47 lines (42 loc) · 1.38 KB
/
mod.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
* The benefit of Options and Result objects is they enable the standardisation
* of interfaces. This removes the need for mixed return types in code which
* reduces the need for type checking. And it reduces the need to throw
* exceptions as errors can be bubbled up.
*
* If a method generates either a string or null instead of returning either
* of these types you can return an Option object.
*
* If a method can succeed or fail instead of returning a result of type X or a
* failure message of type Y you can return a Result object.
*/
import { Result, Ok, Err } from "./src/result.ts";
import { Opt, None, Some } from "./src/option.ts";
export { Panic } from "./src/panic.ts";
/**
* Returns the Some option containing the value to be returned.
*/
export function some<T>(something: T): Opt<T> {
return new Some<T>(something);
}
/**
* Returns the None option when no value is available to return.
*/
export function none(): None {
return new None();
}
/**
* Returns the Ok result containing the result value on successful execution of
* the parent method.
*/
export function ok<T>(result: T): Result<T> {
return new Ok<T>(result);
}
/**
* Returns the Err result containing the error message or value when the parent
* method fails to execute.
*/
export function err<T>(error: T): Result<T> {
return new Err<T>(error);
}
export { Result, Err, Opt, Some, None };