Replies: 1 comment
-
I'm supportive of matching the Go defer syntax and approach. We mainly need to prototype this and confirm it doesn't add a lot of complexity. While using defer at the top-level of the script does make sense in some cases, I could also understand restricting it to being used in functions in the first pass. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Another parity keyword added from Go.
I suggest the exactly same semantics, meaning:
defer
statementEdge cases
Outside any function
When called in the main file outside any function, the deferred call should be executed at the end of the entire program.
Use in REPL
When used in the Risor REPL, it should be called when exiting. If it isn't already, the REPL needs to start listening for Ctrl+C and react to it, instead of just exiting.
Inside imports
To reduce hidden behavior, when called outside any function inside an imported file, it should be called at the end of import.
Motivation
Useful when working with
io.Closer
types, such asos.create()
:Alternatives
Just to get some perspective. I'm personally in favor with the proposal I made above.
Python
with
statementPython's
with
statement is quite enticing, but I've a personal reluctant feeling for it just because of the extra code block, and the fact that the variable name is at the end.On the other hand, a benefit is that you get full control over when the scope ends.
With Risor, it would probably look something like this:
Downside is that you can then only use this on
io.Closer
types.C#
using
statementC# has a
using var ...
declaration that is a simplifiedusing () { }
statement. It callsDispose()
on the object at the end of the block.The nice thing about this shorter version is that it doesn't add extra indentation, similar to Go's
defer
keyword.In Risor, it could look something like this:
Beta Was this translation helpful? Give feedback.
All reactions