Home > OS >  Dictionary bracket syntax not allowed in "match" statements
Dictionary bracket syntax not allowed in "match" statements

Time:11-10

Trying to use dictionary bracket syntax in match statements and it doesn't let me. Is this intentional by design, or am I missing something?

Below doesn't work

match curr:
    myDict["one"]:
        #do something
    myDict["two"]:
        #do something

I am forced to use the dot syntax instead and it works...is this by design?

CodePudding user response:

I can't conclude that it is intentional, but I can tell you it does not work and will not work.

In Godot 3, the parser does not support this syntax, which might have been oversight. Or it might have been that the parser was already becoming a mess and hard to maintain, so features that weren't critical weren't considered? Perhaps… After all, GDScript was reworked from the ground up for Godot 4. So…

In Godot 4, the compiler does not support it, and there is a reason: it wants a constants, which also allow some optimizations. Does Godot 3 care about that? Nope, you can use variables, and there is no problem. And no, the match is not optimized in Godot 3, nothing is, it is all interpreted.

Do you really care if it was intentional?


You are likely OK to do this with a bunch of if statements. After all, if you are willing to write a case for each item in the dictionary, they probably are a manageable ammount.

You could also throw design patterns at it. The strategy pattern comes to mind.

  • Related