-
-
Notifications
You must be signed in to change notification settings - Fork 21.1k
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: Implement pattern guards for match statement #80085
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code looks good to me! 👍 There is a little hack with the pattern guard is SuiteNode
, not ExpressionNode
. In theory, we can get unrelated warnings or something like that, but I did not find any related bugs when I tested it.
The tests are good, but maybe we should add some more? For example a test that an assignment is not allowed inside a pattern guard.
I'm still not sure about the syntax, so I suggest discussing this at the next GDScript team meeting.
I noticed that the pattern guard is executed even if none of the patterns matched. func f() -> bool:
print("f")
return true
func _ready() -> void:
var x := 1
match x:
2 -> if f():
print("A")
_:
print("B") Prints:
|
98c0f12
to
f1b35c6
Compare
Fixed now. Added a test for this case as well as the one for assignment. |
Discussed during a GDScript Team Meeting: this PR will be modified my @vnen to use the |
Within a match statement, it is now possible to add guards in each branch: var a = 0 match a: 0 when false: print("does not run") 0 when true: print("but this does") This allows more complex logic for deciding which branch to take.
f1b35c6
to
54a1414
Compare
I updated the PR to use then new |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me and works great, it won't even break compatibility if someone uses when
as an identifier. Thank you George!
var when = 1
match when:
when when when: # I like it.
when
Thanks! |
Within a match statement, it is now possible to add guards in each branch:
This allows more complex logic for deciding which branch to take.
Closes godotengine/godot-proposals#4775
EDIT: Changed the
-> if
separator to the new keywordwhen
, as discussed.