Switch case error Unrecognized pattern:

I get the error

Source/myapp/Main.hx:110: characters 7-34 : Unrecognized pattern: sm.INPUT_STATE + sm.PERSIST

while using switch case

     	switch (someValue)
    	{
     		case  Std.string(sm.INPUT_STATE + sm.SETTINGS):

           }

I have to write it like this, which is very long. How can I correct the previous code?

var inputStateSettings_Str:String = Std.string(sm.INPUT_STATE + sm.SETTINGS);
     	switch (someValue)
    	{
     		case inputStateSettings_Str:

           }

Perhaps you can use an “if” statement?

Yes, that is what I did ultimately. If Else becomes quite messy sometimes that’s why switch/case becomes preferrable.

I have to write it like this, which is very long.

That doesn’t do what you think it does. Since local variables aren’t valid patterns, case inputStateSettings_Str declares a capture variable and will match anything. That means that the var inputStateSettings_Str before the switch is actually an unused variable.

class Test {
    static function main() {
        var foo:String = "foo"; // can be commented out since it's unused
        switch "bar" {
            case foo: trace("foo"); // hits this code path
            case "bar": trace("bar");
        }
    }
}

https://try.haxe.org/#66CF9

If you’re using VSCode, there’s multiple things that hint at this - both foo variables are greyed out since they’re unused, and any case after the catch-all has a “This case is unused” warning: