-
Notifications
You must be signed in to change notification settings - Fork 28
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 protect function, as alternative to missing try finally construct #155
base: main
Are you sure you want to change the base?
Conversation
It would make more sense to just add the missing |
Sorry, missed this! Will try and get back to this in the coming days. It's true we miss a solid way of emulating |
@glennsl in my mind this is a good solution. There's of course the option to bake syntax sugar for this into the language itself as well with a proper finally keyword etc like @Sod-Almighty mentions above, but I'm not sure it's worth the added complexity, adding a new keyword, etc. We can think about it some more, and adding this doesn't exclude possibly adding that in the future. @Sod-Almighty no idea if you're trying to be funny with your strangely worded message or how to interpret it. |
As a side comment, extending the language with a finally clause would require actual design (it's not just a |
@glennsl I say we go forward with this. I wonder about the naming though. Can we come up with something more illustrative, now that we're going for familiarity for JS devs rather than OCaml? I don't have any good ideas myself yet, do you have any off the top of your head? |
In another thread I asked about If ReScript is a real language with a real parser – written in OCaml or whatever else – and compiles to JS rather than to OCaml, then OCaml's lack of a |
Feel free to come up with competent comments next time. |
Feel free to stop being snide. |
|
@glennsl I like |
Looking at the example, I don't think try protect(~finally=() => Js.log("finally"), () => failwith("oh no!")) catch {
| Failure(err) => Js.log(err)
}
|
try withFinally(
() => failwith("oh no!"),
~finally=() => Js.log("finally"),
) catch {
| Failure(err) => Js.log(err)
} I guess I'm not super set on |
I like the |
So there are two major use cases for this that I can see. One is where you just want to make sure that some code is run whether or not an exception is raised, but you don't want to handle the exception immediately. This is usually in order to clean up some resource, and in that case I think The other is where you want to also handle raised exceptions immediately, in which case you need to use it with the Seems hard to find a good name that covers both scenarios well. |
How about (Sorry was that a "competent comment"? It's so hard to tell.) |
For me, |
@Sod-Almighty I suggest you drop the attitude right now if you hope to ever be taken seriously. You have your chance now to drop it and move on. Yeah definitively tricky. @glennsl makes a good point with the two use cases and how they differ in naming. I like the Sounds like we need to mull on this a bit more. |
You guys started it by mocking me when all I did was ask a question. In any case, I got my answer: unfinished language and toxic devs. I'm out. |
`protect(~finally, f)` | ||
|
||
Tries to execute the function `f`, and ensures that `finally` will be called | ||
whether `f` raises an exception or not. | ||
|
||
Any exception raised by `f` will be re-raised in order to be handled by the | ||
user. If `finally` raises, then that exception will be emitted instead, and | ||
any exception raised by `f` will go unnoticed. | ||
|
||
```res example | ||
try protect(~finally=() => Js.log("finally"), () => failwith("oh no!")) catch { | ||
| Failure(err) => Js.log(err) | ||
} | ||
``` |
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.
Glenn, just a little formatting
`protect(~finally, f)` | |
Tries to execute the function `f`, and ensures that `finally` will be called | |
whether `f` raises an exception or not. | |
Any exception raised by `f` will be re-raised in order to be handled by the | |
user. If `finally` raises, then that exception will be emitted instead, and | |
any exception raised by `f` will go unnoticed. | |
```res example | |
try protect(~finally=() => Js.log("finally"), () => failwith("oh no!")) catch { | |
| Failure(err) => Js.log(err) | |
} | |
``` | |
`protect(~finally, f)` tries to execute the function `f`, and ensures that | |
`finally` will be called whether `f` raises an exception or not. | |
Any exception raised by `f` will be re-raised in order to be handled by the | |
user. If `finally` raises, then that exception will be emitted instead, and | |
any exception raised by `f` will go unnoticed. | |
## Examples | |
```rescript | |
try protect(~finally=() => Console.log("finally"), () => panic("oh no!")) catch { | |
| Failure(err) => Console.log(err) | |
} |
As pointed out in this SO question, there is no built-in try finally construct, which is confusing to people coming from JS.
protect
has existed in OCaml since 4.08, so figured it ought to exist here as well.Not sure if it should be in
RescriptCore
though, or perhaps inError
instead?