Last active
August 8, 2017 10:27
-
-
Save bramblex/fde8b45a29134ffc79cb238cf8d50f95 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 'use strict' | |
| export type Result<T, E> = Succ<T> | Fail<E> | |
| export class Succ<T> { | |
| readonly cont: T | |
| readonly ok: true = true | |
| constructor(cont: T) { this.cont = cont } | |
| } | |
| export class Fail<E> { | |
| readonly err: E | |
| readonly ok: false = false | |
| constructor(err: E) { this.err = err } | |
| } | |
| export function isResultSucc<T, E>(result: Result<T, E>): boolean { | |
| return (result instanceof Succ) | |
| } | |
| export function failResult<T, E>(err: E): Result<T, E> { | |
| return new Fail(err) | |
| } | |
| export function succResult<T, E>(cont: T): Result<T, E> { | |
| return new Succ(cont) | |
| } | |
| export function getErr<T, E>(result: Result<T, E>): E { | |
| if (result instanceof Fail) { | |
| return result.err | |
| } | |
| else { | |
| throw new Error('不能对一个对的结果使用getErr') | |
| } | |
| } | |
| export function getCont<T, E>(result: Result<T, E>): T { | |
| if (result instanceof Succ) { | |
| return result.cont | |
| } | |
| else { | |
| throw new Error('不能对一个错误的结果使用getCont') | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment