Skip to content

Instantly share code, notes, and snippets.

@bramblex
Last active August 8, 2017 10:27
Show Gist options
  • Select an option

  • Save bramblex/fde8b45a29134ffc79cb238cf8d50f95 to your computer and use it in GitHub Desktop.

Select an option

Save bramblex/fde8b45a29134ffc79cb238cf8d50f95 to your computer and use it in GitHub Desktop.
'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