-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add intoTuple #12
Add intoTuple #12
Conversation
726af42
to
fa6f47f
Compare
I think we have to let the user narrow the type in this case. Using I think we should use null, rather than undefined. Thoughts? |
Sounds good, I agree.
I really don't have a strong opinion here, and I see that Will submit these updates shortly and put the PR up for review. |
I have amended the commit with the following changes:
expect(Err(null).intoTuple()).to.deep.equal([null, null]); Which does not seem very useful, especially for the destructuring pattern desired. |
Make doc-comment for `intoTuple` consistent.
There was still a small mistake in the doc-comment which references The const x: Result<User, Error | null> = fetchUser();
const [err, res] = x.intoTuple();
if (res) {
console.log(`Welcome ${res.username}`);
} else if (err) {
console.error(err);
} else {
console.log('User not found');
} Do I like that pattern? Not really - but if I had decided to use it, I would not expect a library which is otherwise un-opinionated to prevent me from doing so. |
Adds the method
intoTuple
toResult
.intoTuple
returns a tuple[undefined, T]
, or[Err<E>, undefined]
if the result isErr
.In the
Err
case, in order to allow for anif (err)
comparison, the err member is wrapped inErr
.There is a question of whether the Err case tuple should contain an unwrapped error instead of
Err<E>
. One problem that may arise when returning an unwrapped err value is when is when it is a falsy value. In that case, this pattern would be unreliable:I can come up with a couple of approaches:
[Err<E>, undefined]
. The caller can then callerr.unwrapErr()
within theif (err)
clause (As demonstrated in this PR)err !== undefined
.err
valueInterested to hear ideas! @traverse1984 Thank you again for your time and feedback.