-
-
Notifications
You must be signed in to change notification settings - Fork 97
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
GDScript add with
context management syntax
#3490
Comments
See also #623. |
@Calinou This are actually two completely different withs. The one here is about cleanup after leaving a scope. The one you linked is about a shortcut for accessing member variables/methods. Other than both use the same keyword they have nothing in common. |
File would have to be handled somewhat differently, since you need to pass one or more arguments. Could still use the same mechanism thought. I imagine something like this: func file_demo() -> Error:
with var f = File.new("some filename", File.WRITE):
f.store_string("something")
return OK In this example NOTIFICATION_ENTER_CONTEXT would remain unused but NOTIFICATION_EXIT_CONTEXT would be used for closing. This proposed syntax also includes a way to add a named variable. This would be equivalent of the Python keyword |
About the conflict with that other proposal that also uses |
I am hoping this will be considered again. Having a reliable way to clean up after a block of code is a big win. Python uses:
and GGScript does seem to be largely based on Python. But anything that will do the job will be welcome. |
Describe the project you are working on
A game that uses mutexes extensively, but this applies to any project requiring context management such as file open/close.
Describe the problem or limitation you are having in your project
Objects that require cleanup after use can be tricky to use safely. Example from #3486:
This applies to file open/close, and possibly other situations. See comment: #3486 (comment)
Describe the feature / enhancement and how it helps to overcome the problem or limitation
Add the pythonic
with
syntax (see https://www.python.org/dev/peps/pep-0343/)This way, the above code could be reimplemented like so:
(This conflicts with #623)
Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams
There are several ways to implement this. Here's a suggestion:
Add
NOTIFICATION_ENTER_CONTEXT
,NOTIFICATION_EXIT_CONTEXT
to Object.When the object is passed to the
with
scope, call this notification immediately. When the scope expires, notify EXIT_CONTEXT.In Mutex, File, support these notifications by adding a call to lock/unlock, open/close.
To ensure that contexts are released in case of an error, I expect that there would have to be much more intrusive changes to GDScript.
This would supercede #3486 (and associated PR godotengine/godot#54444).
If this enhancement will not be used often, can it be worked around with a few lines of script?
From #3486, a hacky way to do this, using Mutex as an example:
Is there a reason why this should be core and not an add-on in the asset library?
GDScript is part of core. The workaround described above is not as performant.
The text was updated successfully, but these errors were encountered: