3

Is there a way like in C# to create a switch with multiple variables? something like this..

 switch((_dim1,_dim2,_dim3))
 {
        case(1, ""):
            Info("1 is null");
        case (2, ""):
            Info("2 is null");
        case (3, ""):
        break;
 }
Jan B. Kjeldsen
  • 17,817
  • 5
  • 32
  • 50
Tweene
  • 257
  • 4
  • 16

2 Answers2

5

Switch with multiple arguments have been possible since version 1.0 (+23 years). It requires the use of containers. There is no dont-care value.

The following job demonstrates:

static void TestConSwitch(Args _args)
{
    int dim1 = 2;
    str dim2 = '';    
    switch ([dim1, dim2])
    {
        case [1, '']:
            info("Case 1");
            break;        
        case [2, '']:
            info("Case 2");
            break;
        default:
            info("Default!");
    }
}

In this case displays "Case 2".

That said, performance of this may not be great, as it requires the build of containers for both the target value and all the case values (if no match). Looks kind of neat though.

Jan B. Kjeldsen
  • 17,817
  • 5
  • 32
  • 50
1

This is not possible in , as there is no tuple pattern as in .

Edit: As @Jan B. Kjeldsen shows in his answer, containers can be used similar to how tuple patterns can be used in to create a switch statement with multiple inputs.

FH-Inway
  • 4,432
  • 1
  • 20
  • 37