-
Notifications
You must be signed in to change notification settings - Fork 42
Python-like context management #200
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
Comments
That's already a thing. Declaring a variable with do
close x = {
<>: {
__close: () =>
print("Closing #{@}...")
}
}
print("Foo")
error("Bar") ... the following will show when running it:
Sidenote: This is a feature from regular Lua 5.4, where it is written as |
Good to know, thanks! But we almost there:
This would be more concise without helper variables:
What do you think, is this reasonable request? |
P.S. |
You can just do this: do
close f = io.open(filename)
r::read("*a") This also has the added advantage that you can easily use the read value by simply writing
The problem here is that you'd be introducing a compatibility issue the other way around. Now, if a third-party library has You could consider simply putting something like this at the entrypoint of your app and avoid that headache: do
const f = assert(io.open("/dev/null"))
f.<"__close"> ??= () => @close()
f::close() |
It's not clear to me why would I ever need
I tend to agree that for now it is the best option. Still I'd find the following more expressive: using f = io.open(filename)
r::read("*a") |
Ok, as I see What if allow to write assignment in the same line as do: do close f = io.open(filename)
f::read("*a") And allow if close f := io.open(filename)
f::read"*a" |
+1 for this. IMO, the walrus operator should not be needed here, since the
where |
As explained for example here: https://www.geeksforgeeks.org/with-statement-in-python/
As "with" keyword is not available, we could use "using" instead, like in C#
The text was updated successfully, but these errors were encountered: