-
Notifications
You must be signed in to change notification settings - Fork 111
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
Update try-expression. #197
Conversation
Synchronize syntax and compilation with the original compiler: * Allow empty body in catch and finally clauses. * Allow parameter-less catch. * Don't allow `then` after `try` or `finally`. * Imply `catch` only for a lone try clause (without `finally`).
Merged as 37a7cef. Thanks. |
Regarding the clean-up: Something like Example (with the clean-up):
compiles to:
The last test does not work as i (naively) assumed and fails silently. Compare:
While the |
@marchaefner: I can't seem to create a failing test case. Never mind, I was wrong. |
@marchaefner: Thanks for the persistence. I think I've fixed it with 8ec74fd. Can you try that out for me? |
Great. A much better solution. There only remains the issue with a dangling |
@marchaefner: A dangling |
Yes, but:
currently compiles to
It should either:
Aye (see 2nd compilation suggestion). But i guess this should be done by the optimiser. |
@marchaefner: |
I would rather say that only a lone |
Alright, that's plausible, though I would say unlikely. I'd like to hear the opinion/wisdom of @jashkenas, @Nami-Doc, or @satyr on this. |
Not sure I have much wisdom to bring, but I think that a try implies an empty catch in every case. I admit I never saw |
Even github searching only brings keyword lists and a lot of tests. |
@Nami-Doc: We all agree on that. The question is whether a |
I think you missed my edit (since github doesn't "refresh" posts)
|
Okay, re-closing until someone else makes noise about this. I think we've fixed what was a bug in jashkenas/coffee-script. I'll document it along with the other intentional deviations from jashkenas/coffee-script on the wiki. |
I'd agree. We should fix it there as well. |
This change will break any code that's using a try { use a resource } finally { clean up the resource } pattern by silently absorbing any errors instead of throwing them. Given that in javascript and in historical coffeescript, try...finally without a catch does not absorb errors, my guess is that this would be very surprising behavior. (I'd have to revisit all my code that uses finally statements since about half the time I deliberately omit the catch statement). Implicitly creating catch statements seems like a dangerous default, especially since it's rarely the correct behavior to discard thrown errors without any handling at all. This seems like something javascript got right... Rather than implicitly creating catch, I'd rather a try without a catch or finally be a syntax error. |
I totally agree with @jphaas. Slightly contrived example: initializeAudioPlayback: ->
try
# any of these functions can throw an exception
@checkBrowserCapabilities()
@decodeAudioData()
@schedulePlayback()
catch e
console.log('cannot initialize playback: ' + e);
trigger 'error', e.message
checkBrowserCapabilities: ->
try
# try creating and manipulating DOM objects
finally
# remove any created objects from the DOM
# etc. Generating implicit |
@jashkenas: Were we wrong here to change this behaviour? I try to never write code like the above (mutation and complex control flow, blech), but it seems some people would find use in catch-less try-finally constructs. I think that we might want to err on the side of caution and revert the behaviour back to how it was. |
Agreed. |
@michaelficarra, I don't think that the above example is a good code, but it is a valid use case, however. Thank you for reverting this behavior! |
Okay, after looking into this deeper, I see where the mistake was made. When @jphaas resurrected this thread, he had missed the fact that a |
To conclude jashkenas/coffeescript#2900, this PR brings the try-expression into line with CS1:
catch
only for a lone try clause (withoutfinally
).then
aftertry
orfinally
.