<- ^ ->
Pattern matching basic types

12   Pattern matching basic types

Pattern matching is technique used to decomposite complex datatypes and made decisions based on their content.

In Gont one might pattern-match ints, strings, bools, tuples and unions. Pattern matching ints and bools looks like switch in plain C:

        string my_itoa(int i)
        {
                switch (i) {
                case 1:   return "one";
                case 2:   return "two";
                case _:   return "infinity";    // it's set rather low...
                }
        }
case _: is Gont way to say default:. It is called match-all pattern.

As a consequence of string being built in, basic type, strings can also be pattern matched:

        int my_atoi(string s)
        {
                // note lack of () around s, they can be omitted
                switch s {
                case "one": return 1;
                case "two": return 2;
                case _:     return 3; 
                }
        }
As you probably guessed, bool's are pattern matched with true and false.

<- ^ ->
Pattern matching basic types