Converts from Result<T, E>
to Option
Converts result into an Option
Basic usage:
const x: Result<number, string> = Ok(2);
assert.deepEqual(x.err, None());
const x: Result<number, string> = Err('Nothing here');
assert.deepEqual(x.err, Some('Nothing here'));
Returns true
if the result is Err
.
Basic usage:
const x: Result<number, string> = Ok(-3);
assert.equal(x.isErr, false);
const x: Result<number, string> = Err('Some error message');
assert.equal(x.isErr, true);
Returns true
if the result is Ok
.
Basic usage:
const x: Result<number, string> = Ok(-3);
assert.equal(x.isOk, true);
const x: Result<number, string> = Err('Some error message');
assert.equal(x.isOk, false);
Converts from Result<T, E>
to Option
Converts result into an Option
Basic usage:
const x: Result<number, string> = Ok(2);
assert.deepEqual(x.ok, Some(2));
const x: Result<number, string> = Err('Nothing here');
assert.deepEqual(x.ok, None());
Returns res
if the result is Ok
, otherwise returns the Err
value of self.
Basic usage:
const x1: Result<number, string> = Ok(2);
const y1: Result<string, string> = Err('late error');
assert_eq!(x1.and(y1), Err('late error'));
const x2: Result<number, string> = Err('early error');
const y2: Result<string, string> = Ok('foo');
assert.deepEqual(x2.and(y2), Err('early error'));
const x3: Result<number, string> = Err('not a 2');
const y3: Result<string, string> = Err('late error');
assert.deepEqual(x3.and(y3), Err('not a 2'));
const x4: Result<number, string> = Ok(2);
const y4: Result<string, string> = Ok('different result type');
assert.deepEqual(x4.and(y4), Ok('different result type'));
another result
Calls cb
if the result is Ok
, otherwise returns the Err
value of self.
This function can be used for control flow based on Result values.
Basic usage:
type Fn = (x: number) => Result<number, number>
const sq: Fn = x => Ok(x * x);
const err: Fn = x => Err(x);
assert.deepEqual(Ok(2).andThen(sq).andThen(sq), Ok(16));
assert.deepEqual(Ok(2).andThen(sq).andThen(err), Err(4));
assert.deepEqual(Ok(2).andThen(err).andThen(sq), Err(2));
assert.deepEqual(Err(3).andThen(sq).andThen(sq), Err(3));
callback to map wrapped value
Unwraps a result, yielding the content of an Ok
.
Throws an error if the value is an Err
, with a error message including the passed message,
and the content of the Err
.
Basic usage:
const x: Result<number, string> = Err('emergency failure');
x.expect('Testing expect'); // panics with `Testing expect: emergency failure`
additional custom error message
Maps a Result<T, E>
to Result<U, E>
by applying a function to a contained Ok
value,
leaving an Err
value untouched.
This function can be used to compose the results of two functions.
Print the numbers on each line of a string multiplied by two.
let line = '1\n2\n3\n4\n';
const parse = (num: string): Result<number, string> => {
const parsed = parseInt(num, 10);
return parsed
? Ok(parsed)
: Err('error');
};
for (const num of line.split('\n')) {
parse(num).map(i => i * 2).match({
Ok: v => console.log(v),
Err: () => {}
});
}
callback to map wrapped value
Maps a Result<T, E>
to Result<T, F>
by applying a function to a contained Err
value,
leaving an Ok
value untouched.
This function can be used to pass through a successful result while handling an error.
Basic usage:
const stringify = (x: number) => `error code: ${x}`;
const x: Result<number, number> = Ok(2);
assert.deepEqual(x.mapErr(stringify), Ok(2));
const y: Result<number, number> = Err(13);
assert.deepEqual(y.mapErr(stringify), Err('error code: 13'));
callback to map error value
Matches result to the received matcher and executes appropriate branch.
Basic usage:
const x1 = Ok(10);
const y1 = x1.match({
Ok: v => v + 10,
Err: () => 100
});
assert.equal(y1, 20);
const x2 = Err('error');
const y2 = x2.match({
Ok: v => v + 10,
Err: () => 100
});
assert.equal(y2, 100);
const x3 = Err('error');
x3.match({
Ok: v => console.log(v),
Err: e => console.log(e + 's') // prints "errors"
});
an object to perform pattern matching
Returns res
if the result is Err
, otherwise returns the Ok
value of self.
Basic usage:
const x1: Result<number, string> = Ok(2);
const y1: Result<number, string> = Err('late error');
assert.deepEqual(x1.or(y1), Ok(2));
const x2: Result<number, string> = Err('early error');
const y2: Result<number, string> = Ok(2);
assert.deepEqual(x2.or(y2), Ok(2));
const x3: Result<number, string> = Err('not a 2');
const y3: Result<number, string> = Err('late error');
assert.deepEqual(x3.or(y3), Err('late error'));
const x4: Result<number, string> = Ok(2);
const y4: Result<number, string> = Ok(100);
assert.deepEqual(x4.or(y4), Ok(2));
default result
Calls cb
if the result is Err
, otherwise returns the Ok
value of self.
This function can be used for control flow based on result values.
Basic usage:
type Fn = (x: number) => Result<number, number>
const sq: Fn = x => Ok(x * x);
const err: Fn = x => Err(x);
assert.deepEqual(Ok(2).orElse(sq).orElse(sq), Ok(2));
assert.deepEqual(Ok(2).orElse(err).orElse(sq), Ok(2));
assert.deepEqual(Err(3).orElse(sq).orElse(err), Ok(9));
assert.deepEqual(Err(3).orElse(err).orElse(err), Err(3));
callback to calculate default result
Unwraps a result, yielding the content of an Ok
.
Throws an error if the value is an Err
, with a error message provided by the Err
's value.
Basic usage:
const x: Result<number, string> = Ok(2);
assert.equal(x.unwrap(), 2);
const x: Result<number, string> = Err('emergency failure');
x.unwrap(); // fails with `emergency failure`
Unwraps a result, yielding the content of an Err
.
Throws an error if the value is an Ok
, with a custom error message provided by the Ok
's
value.
Basic usage:
const x: Result<number, string> = Ok(2);
x.unwrapErr(); // panics with `2`
const x: Result<number, string> = Err('emergency failure');
assert.equal(x.unwrapErr(), 'emergency failure');
Unwraps a result, yielding the content of an Ok
. Else, it returns optb
.
Basic usage:
const optb = 2;
const x: Result<number, string> = Ok(9);
assert.equal(x.unwrapOr(optb), 9);
const y: Result<number, string> = Err("error");
assert.equal(y.unwrapOr(optb), optb);
default
Unwraps a result, yielding the content of an Ok
. If the value is an Err
then it calls cb
with its value.
Basic usage:
const count: (x: string) => number = x => x.length;
assert.equal(Ok(2).unwrapOrElse(count), 2);
assert.equal(Err("foo").unwrapOrElse(count), 3);
callback to calculate default value
Generated using TypeDoc
Result
is a type that represents either success (Ok
) or failure (Err
).See the result module documentation for details.