Does doSwitch evaluate regex patterns in possibilities ?

Hi
I have a query regarding functioning of doSwitch.

I have a certain value with regard to which I want to take 2 actions depending on the pattern that value returns.

doSwitch("${myKey}")(
key1 → chain1,
key2 → chain2
)

e.g. myKey will hold a alphabetical value → abc or numerical value-> 542
And I want my keys to basically match that pattern. But doSwitch is not evaluating the regex

My Code →

doSwitch("${myKey}")(
“”"[a-z]+""".r → chain1,
“”"[0-9]+""".r → chain2
)

This is not working . But if I write the exact values as:

doSwitch("${myKey}")(
“”“abc”"" → chain1,
“”“542"”" → chain2
)
The code works.

Can anyone help me here?

Hi,

The doSwitch is currently implemented with a Map. So we use direct value, not pattern matching in this case.

As you only have 2 branches, a simple doIfOrElse may work:

doIfOrElse(session => session("myKey").as[String].matches("[0-9]+")) {
  chain1
} {
  chain2
}

If you have more branches, precompute the descriminator and switch over it.

Cheers,

Thanks Sébastien for the reply.
I want this to work over multiple branches. Would need to precompute only.