/**
 * Functor Try
 */ export class Success < T > { _value : T ; constructor ( init ) { this . _value = init ; } static of < T > ( init ) { return new Success ( init ) ; } map < U > ( func ) { return Success . of ( func ( this . _value ) ) ; } value ( ) { return this . _value ; } } export class Failure < T > { _value : T ; constructor ( init ) { this . _value = init ; } static of < T > ( init ) { return new Failure ( init ) ; } /**
     * Failure 的 map 不作任何处理
     */ map ( _ ) { return this ; } value ( ) { return this . _value ; } } /**
 * Try 函子，98 错误处理定制版
 */ export class Try < S , F > { _value : Success < S > | Failure < F > ; /**
     * 标识是否已经进行过 fail 处理
     */ _hasHandleErr : boolean = false ; constructor ( init ) { this . _value = init ; } static of < S , F > ( init ) { return new Try < S , F > ( init ) ; } param func map < U > ( func ) { if ( this . _value . constructor === Success ) { return Try . of < U , F > ( ( this . _value as Success < S > ) . map ( func ) ) ; } return Try . of < U , F > ( ( this . _value as Failure < F > ) . map ( id => id ) ) ; } /**
     * 是否成功
     */ isSuccess ( ) { return this . _value . constructor === Success ; } /**
     * 成功回调
     */ succeed ( func ) { if ( ! this . _hasHandleErr ) { throw new Error ( '[Try] You must use `fail` to handle error first.' ) ; } if ( this . _value . constructor === Success ) { func ( ( this . _value as Success < S > ) . value ( ) ) ; } } /**
     * 错误处理
     */ fail ( errHandleFunc ? ) { this . _hasHandleErr = true ; if ( this . _value . constructor === Failure ) { errHandleFunc && errHandleFunc ( ( this . _value as Failure < F > ) . value ( ) ) ; } return this ; } }