optionals
optionals copied to clipboard
Feature requests: convenience helpers and improved spec compliance
Some features that I'd like to contribute from my personal implementation that this one is missing. Opening issue for discussion before just opening a pull request to help eliminate unnecessary work. Some of these are convenience, some of these bring the implementations closer to the Rust spec. Porting these is little effort, as the code already exists in my own personal work.
Convenience helpers
- [ ]
function fromPromise<V, E extends Error>(promise: Promise<V>): Promise<Result<V, E>>; avoids additional microtasks - [ ]
function fromSync<V, E extends Error>(func: () => V): Result<V, E>; synchronous helper, avoids microtasks completely - [ ]
Symbol.toStringTagsupport; useful to quickly and easily distinguish resulting objects - [ ]
Symbol.hasInstancesupport onErr,Ok,Some,None; examplemyResult instance of Err
Spec compliance
- [ ] Iterable support, similar to
class Result<T, E extends Error> implements Iterable<T> - [ ]
Result.expectErr
Hey man, thanks for opening an issue I really appreciate the interest! Here are my thoughts:
Convenience helpers
- I definitely agree about separating out synchronous and asynchronous
froms. I think the best naming convention for this would be to havefromas the sync method andfromPromiseas the async method. Symbol.toStringTag, could you give an example of how this might be used?Symbol.hasInstance, 100% that's a good shout.
Spec Compliance
- Iterable support, what would an implementation of this look like?
Result.expectErr, again this is definitely a good inclusion.
Thanks for getting back to me! I appreciate the positive response and feedback, that's cool.
Convenience helpers
- Hard agree on this point; one reason I had for suggesting an alternate naming conventions is that an async
fromalready exists and I was concerned with breaking changes. If braking changes are okay, I think this is a welcome adjustment to the API. Symbol.toStringTagis used by JS engines when callingObject.toStringcalls; would produce[object Err]for example instead of[object Object], which is useful during debugging and with runtime checks some folks use.Symbol.hasInstancecool.
Spec Compliance
- Iterable support allows easy access to the value if any; a
for...ofwould have either a single iteration or no iteration, andArray.fromcould be used. It's a type of transposition pattern in Rust which I find useful. Could be either an implicitimplements Iterable<T>which would be JS "native" or anitermethod like Rust Result.expectErr, nice.
@aaronhuggins I have opened a PR with all of the above changes except Symbol.hasInstance.
What did you have in mind for this? Converting Ok, Err and Some to symbols?
Oh! I was literally planning on opening the PR and contributing my existing code. :laughing:
I'll get you my code samples or get you another PR if you want.
Oh sorry! 😆
Yeah if you could provide some snippets that would be great!
I'll add as comments in PR.