-
Notifications
You must be signed in to change notification settings - Fork 30
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
Option additions #57
base: main
Are you sure you want to change the base?
Option additions #57
Conversation
6a2c171
to
a47fef5
Compare
I'd also like to rename |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@glennsl Thanks for all the work you are doing!
| None => raise(Not_found) | ||
} | ||
|
||
let expect = (opt, message) => |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if we shouldn't rather leave the implementation of such a function to e.g. testing frameworks (which might also want to raise a different exception).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't for testing. You should consider using this every time you reach for getExn
because it gives you a better, easier to track down error message when the assumption that lead you to using it inevitably fails in production 🙃
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One thing that comes to mind here - do we need an additional alternative where you can pass your own exception? I recall people in the community talking about adding that themselves.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the use case for that? To have a distinct exception that they can catch?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, exactly. But let's ignore it for now, feels peripheral.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, right, so there is definitely demand in the community for something like this.
And what do you think about the naming? Usually we have exn
in the method name when the function might raise. That wouldn't be the case with expect
. Maybe it should be getExnWithMessage
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that would ensure that absolutely no-one would pick this over getExn
, unfortunately 😉
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
expectExn
could be an alternative. Though I don't think the convention is that functions that raise should always use Exn
, but rather that it signifies a variant of a function that distinguishes itself by raising. Similar to how not all functions that can return an option uses Opt
.
Is there not an annotation that can be put on functions that raise? I thought reanalyze utilized that, but I can't find it now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Naming this one is hard for sure... I'm trying to come up with a good alternative as well. I like expect
because I've used it a lot in Rust, but I'm not sure it'd feel very familiar to people who haven't used Rust. I'm ok with getExnWithMessage
, but I understand if it feels too verbose. So let's maybe think a round or two more about what could be a good alternative name, before we decide.
@glennsl correct, it's @raises
: https://rescript-lang.org/syntax-lookup#raises-decorator
We should eventually work our way through all the bindings and ensure all of them have @raises
where appropriate. That'll be a lot of work though, so probably wise to do it little by little, over time. It's also less important than the general documentation is right now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I logged an issue for @raises
.
Still haven't figured out a better name than expect
or getExnWithMessage
. Anyone else got something? Otherwise we'll need to figure out the best choice between those two.
fb08033
to
e62168e
Compare
f810046
to
d4208d8
Compare
So, the only thing we have left is deciding between |
I like |
Ok, let's do it like this: Like this comment if you want it to be called |
And this if you want it to be called |
I think this name is more coherent with the other functions, could also be called |
ffd4291
to
47b16c7
Compare
Didn't see this until tonight. Please see #85 I've surveyed F#, OCaml, Belt, fp-ts and think there are a lot of holes to fill. I'm putting together a proposal. |
Co-authored-by: Christoph Knittel <[email protected]>
Co-authored-by: Pedro Castro <[email protected]>
Co-authored-by: Pedro Castro <[email protected]>
47b16c7
to
9280051
Compare
Naming-wise I prefer flatten to flat. That was one on my tentative list of missing functions. I'll try to get my list together in next 24 hours so we can discuss. |
|
Please beware it's not a goal for this project to cover that ground. That's better done in another standalone library. |
Small thing - when I switched from JavaScript I was happy to see the word "keep" instead of "filter" because it made it super clear what's happening. I still find it a bit confusing whether I am filtering stuff IN or OUT. Shorter word too. I'm not going to fight that battle though. "filter" is the word used almost everywhere and getting people over from JavaScript is the priority. |
Ok see the issue for lots of ideas... #85 |
I don't know if we need these "withMessage" or "expect" features, regardless of what they are called. But I'm curious why we don't use optional labeled parameters instead so we don't pollute the namespace with tons of overrides. |
Optional labeled parameters cannot be put at the end, due to currying. If on function application |
Forgive me if I'm not understanding, but why not...
I just tried it as both these flavors it returns an int, no warnings.
|
Sure, that's a good alternative, though it is a bit more verbose. This style also won't work with bindings. This isn't a binding, of course, but if we're aiming for a consistent style that does work with bindings then this isn't it. Another minor benefit of the |
The first impression of @cknitt was this was for testing. Me too. It sounds very much like |
Ok, adding an optionally labelled argument might actually be the best bet here in my opinion ( someOptArray->Array.map(Option.getExn) ...but. One goal of Core is to be "uncurried by default"-ready, and I believe that the above code would work as is in uncurried by default (cc @cristianoc). |
The problem is that this is not t-first. We should design with v11 in mind, as that changes the possible tradeoffs. This also means that adding certain functions will have to wait. |
Relevant for option design too: rescript-lang/rescript#5628 |
I think it's fine. But I still think |
Our pattern is to group the getters together https://stackoverflow.com/questions/66362625/why-is-rusts-expect-called-expect Eventually I want the Option module to have lots of functions inspired by other languages - Rust even as If we had a labeled optional argument at the end, must the label be used after the curry change? Because then we're still adding If someone really want this feature now here are some naming ideas and you can see how it fits in.
|
A few additions I've found useful. Of note:
expect
is inspired byOption.expect
in Rust, to get more useful error messages.orElse
was originally named as such becauseor
was a reserved keyword. In rescript it no longer is, andorElse
carries a slightly different meaning (lazy construction) for those coming from Rust. It therefore seems like a good idea to useor
for this and reserveorElse
to conform to, or at least not conflict with, the Rust convention.