How to use enumerate in dictionary pattern match?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By freakcoco

I don’t know how to match enumerate value.
Enumerate is constant but I can’t put It into pattern key!

e.g. #error not a constant expression as key

enum { A, B, C}
func test():
    match {A:1,B:2}:
        {A:1, ..}:
            print("match")
        _:
            print("not match")
      
:bust_in_silhouette: Reply From: ATom 1

You should use Array, and also ensure that your enumeration value is a continuous set of positive integers starting from 0 (the default is to sort from 0 in your writing order, provided that you do not set other Integer value for one of enum value), this is the correct way to use enumerations as index values, and is faster than dictionaries. Currently, dictionaries in gdscrip should only use strings as index values.

enum { A, B, C}
func test():
    var arr_test:=[“B_string","C_string","A_string"]
    print(arr_test[A],arr_test[B],arr_test[C]

About “Array pattern”,you should look this:

:bust_in_silhouette: Reply From: ynsdevelop

from docs, the basic syntax is

match [expression]:
    [pattern](s):
        [block]
    [pattern](s):
        [block]
    [pattern](s):
        [block]

As far as i know you must use a variable or function for match expression
i.e:

enum { A, B, C}
func test(my_value):
    match my_value:
        {A:1, ..}:
            print("match")
        _:
            print("not match")