[SalesForce] Good approach to making switch on string values case insensitive

In Apex the == operator is case-insensitive for strings and in other places case insensitivity is supported.

But the recently introduced switch is case sensitive. The workaround of moving to one case compiles for the value:

switch on operation.toLowerCase() {

but not for the comparison constants:

when 'EventActions.fire'.toLowerCase() {

Any good patterns for coding a case insensitive string switch in Apex? Or is it back to if/else if/else?

Best Answer

Switch statements, while welcome, are definitely still limited in functionality. It can't replace all possible situations you'd like to replace if-else statements with switch statements. Depending on your specific situation, you might be able to come up with a workaround. In your specific example, you'd write 'eventactions.fire', of course, but if you were using some other source, then it wouldn't make sense. While it's not necessarily ideal, I'd recommend trying to use switch over not, simply because it is easier. Also, switch will likely be expanded in future releases, so what's not possible today might be in the future.