@@ -7,9 +7,6 @@ interface ITry<A> {
77 ) : Try < A | B > ;
88 finally ( onfinally ?: ( ( ) => void ) | undefined | null ) : Try < A > ;
99
10- unwrap ( ) : A ;
11- result ( ) : Result < A > ;
12-
1310 readonly [ Symbol . toStringTag ] : string ;
1411}
1512
@@ -59,14 +56,6 @@ export class Success<A> implements ITry<A> {
5956 return new Failure ( error ) ;
6057 }
6158 }
62-
63- unwrap ( ) : A {
64- return this . value ;
65- }
66-
67- result ( ) : Result < A > {
68- return { ok : true , value : this . value } ;
69- }
7059}
7160
7261export class Failure < A > implements ITry < A > {
@@ -115,14 +104,6 @@ export class Failure<A> implements ITry<A> {
115104 return new Failure ( error ) ;
116105 }
117106 }
118-
119- unwrap ( ) : A {
120- throw this . error ;
121- }
122-
123- result ( ) : Result < A > {
124- return { ok : false , error : this . error } ;
125- }
126107}
127108
128109export type Try < A > = Success < A > | Failure < A > ;
@@ -135,6 +116,7 @@ export interface TryConstructor {
135116 apply < A > ( value : ( ) => A ) : Try < A > ;
136117 success < A > ( value : A | Try < A > ) : Try < Unwrapped < A > > ;
137118 failure < A = never > ( reason : unknown ) : Try < A > ;
119+ unwrap < A > ( value : Try < A > ) : A ;
138120 isOk ( value : unknown ) : value is Success < unknown > ;
139121 isError ( value : unknown ) : value is Failure < unknown > ;
140122 isTry ( value : unknown ) : value is Try < unknown > ;
@@ -156,18 +138,27 @@ const TryImplementation: Omit<TryConstructor, never> = {
156138 success < A > ( value : A | Try < A > ) : Try < Unwrapped < A > > {
157139 if ( TryImplementation . isTry ( value ) ) {
158140 return TryImplementation . apply (
159- value . unwrap . bind ( value )
160- ) as unknown as Try < Unwrapped < A > > ;
141+ ( ) => TryImplementation . unwrap ( value ) as Unwrapped < A >
142+ ) ;
161143 }
162144 return new Success ( value ) as unknown as Try < Unwrapped < A > > ;
163145 } ,
164146 failure < A = never > ( reason : unknown ) : Try < A > {
165147 if ( TryImplementation . isTry ( reason ) ) {
166- return TryImplementation . apply ( reason . unwrap . bind ( reason ) as ( ) => A ) ;
148+ return TryImplementation . apply (
149+ ( ) => TryImplementation . unwrap ( reason ) as A
150+ ) ;
167151 }
168152
169153 return new Failure ( reason ) ;
170154 } ,
155+ unwrap < A > ( value : Try < A > ) : A {
156+ if ( TryImplementation . isOk ( value ) ) {
157+ return value . value ;
158+ } else {
159+ throw value . error ;
160+ }
161+ } ,
171162 isOk ( value : unknown ) : value is Success < unknown > {
172163 return value instanceof Success ;
173164 } ,
0 commit comments