-
-
Notifications
You must be signed in to change notification settings - Fork 20.2k
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 proposal: 'with' statement #1736
Comments
+1, though you can use if statements instead.... 😄 |
+1. While the "if" check is almost equivalent from a functionality point-of-view, I find the "with" syntax to be much easier to visually parse what is going on. It is also nice that it requires one line instead of two. |
but what if someone cache the reference of node like this. var node = get_node("node")
with node as Node:
# do something
without:
# do something else I think it's the same thing. |
The main advantage of this over the if statement approach is in the aforementioned debugging and refactoring approach, var node = get_node("node")
if node != null:
# do something
else:
print("[Notice]: Unexpected null value")
print_stack() Not the worst thing in the world, but coming from a Python background, I'm a fan of using syntactic sugar to support, and more importantly promote good design patterns. Python's
You'd be correct. Since with get_tree().get_nodes_in_group("Player")[0] as player: quite often. |
with in python is completely different from what you are describing here. The with statement scopes a resources. It replaces: lock.acquire()
try:
....
finally:
l.release() or: f = open("path")
try:
...
finally:
f.close() Using The traditional way of using try/finally obscured the the original intent as the reader would not know if the purpose of the score was catching error or taking care of scope resources. What you are suggesting here is completely different. It's just pure syntactic sugar that serves a very narrow purpose. I would even argue that it's bad pad practice to expect nulls in your nodes except for very special cases. As for
Godot makes things like that hard on purpose as it's an anti-pattern to use that everywhere in your code. You should actually put that in a method on fixed parent in the tree (e.g game, level). That gives the subnode context and makes sure that you can only do get the things you need (like the player) from a logical context like a level where you will most likely find it and not in the options menu where it will probably be null.
Saving one line of code for something that is probably an anti-pattern anyway is not something I'd like to see in gdscript. |
I would argue for "else:" rather than "without:". |
Based on the description above (and the one in #18345 for another use case), I don't think we'll implement any The proposal in this issue is for a kind of sugar which is hardly relevant now that you can use
instead of
If you need |
@Xrayez That said, |
@Calinou thanks, I deleted the message entirely, it was written by me being sleepy at 3 am. I guess I somehow misinterpreted the usage as of: if has_node("node"):
var node = get_node("node") where |
I have a idea I'd like to play around with coding, but I'd like to get people's thoughts before delving into it.
In Godot, it's common to get nodes as such:
Which is fine, until you try using this node
but the node doesn't exist:
This is a common occurrence when nodes are deleted, and this strong binding between nodes can be problematic. If I have a commonly used node (such as the player sprite) and I decide later that I want it to disappear (when dying for example), I now need to scour my code for instances of it to make sure my game won't crash.
Right now, this means wrapping each instance of
get_node()
(or similar) with:What I am proposing is a simplified syntax:
with
would run the statement (get_node
in this case), then if the result isn'tnull
, assign it to the variable afteras
, and run the code block.On the surface, this is just a small bit of syntactic sugar, but it also opens up some possibilities.
The first is automatic debugging facilities. While you may generally want to be able to gracefully delete a node using this pattern, it could also lead to unexpected behavior if overused. As such, a warning could be automatically printed to the console in the case of a null result:
Of course, this might actually be expected behavior, in which case it would be nice to handle it intentionally.
In a sense this would create a case-specific try/except block.
Of course,
with
is a common keyword in other languages, and its usage varies between them, so perhaps another keyword might be preferred (sadly,using
has the same problem). Or perhaps we could expand it to share behavior with the language that (sorta) gave me this idea, and GDScripts main influence, Python, by having it use optionally support construction and deconstruction callbacks (_with_enter
/_with_exit
?) if the statement results in an object. Of course, Python's primary use for those, file handling, may be unnecessary in GDScript.The text was updated successfully, but these errors were encountered: