DXL Equivalent of switch statement

Is there a DXL-equivalent of JavaScript's switch statement?  I want to check a variable against multiple strings and thought that using a switch-style statement would be easier to read (and write) than having multiple OR'ed equal statements.  An example would be something like:

switch (sMyName) {
    case "Jon":
    case "Christopher":
    case "Douglas":
    case "Michael":
        sPrefix = "Mr.";
        break;
    case "Janet":
    case "Monica":
    case "Karen":
    case "Cathy":
        sPrefix = "Ms.";
        break;
}

I know these are a very simplistic list of names, but gets the point across of what I want to do. I'd rather do the above than have:

if (sMyName=="Jon" || sMyName=="Christopher" || sMyName=="Douglas" || sMyName=="Michael") {
    sPrefix = "Mr.";
} else if (sMyName=="Janet" || sMyName=="Monica" || sMyName=="Karen" || sMyName=="Cathy") {
    sPrefix = "Ms.";
}

which could turn out to be a large number of choices and thus become even harder to read.

 

Chris


chrscote - Tue Feb 07 13:34:01 EST 2017

Re: DXL Equivalent of switch statement
Mathias Mamsch - Wed Feb 08 03:46:24 EST 2017

No there is no switch / case statement in DXL. Regarding readability you need to rethink in DXL and use what you have, and make the syntax tools you need for readability / productivity, like: 

// ********** Hide this inside a library *************

// DXL supports variable arguments!
bool inArray(string names[]) {
  if (sizeof names < 2) error "inArray expects at least 2 arguments!"; 
  string search = names[0];
  int i; for (i = 1; i < (sizeof names)-1; i++) 
    if (search == names[i]) return true; 
  return false;
}

// ******************************************

bool isDude (string dude) {
    return inArray(dude, "Jon", "Christopher", "Douglas", "Michael");
}

bool isDudette (string dudette) {
    return inArray(dudette, "Janet", "Monica", "Karen", "Cathy");
}

string sMyName = "Janet"; 

if (isDude sMyName) {
 print "Dude!"; 
} else if (isDudette sMyName) {
 print "Dudette"; 
} else {
 print "What is it?"
}

But plain old if/else is not so bad actually. In the end, everybody reading an if/else knows what it means, with a custom function it can be harder to understand sometimes. Regards, Mathias

Re: DXL Equivalent of switch statement
chrscote - Wed Feb 08 10:23:52 EST 2017

Mathias Mamsch - Wed Feb 08 03:46:24 EST 2017

No there is no switch / case statement in DXL. Regarding readability you need to rethink in DXL and use what you have, and make the syntax tools you need for readability / productivity, like: 

// ********** Hide this inside a library *************

// DXL supports variable arguments!
bool inArray(string names[]) {
  if (sizeof names < 2) error "inArray expects at least 2 arguments!"; 
  string search = names[0];
  int i; for (i = 1; i < (sizeof names)-1; i++) 
    if (search == names[i]) return true; 
  return false;
}

// ******************************************

bool isDude (string dude) {
    return inArray(dude, "Jon", "Christopher", "Douglas", "Michael");
}

bool isDudette (string dudette) {
    return inArray(dudette, "Janet", "Monica", "Karen", "Cathy");
}

string sMyName = "Janet"; 

if (isDude sMyName) {
 print "Dude!"; 
} else if (isDudette sMyName) {
 print "Dudette"; 
} else {
 print "What is it?"
}

But plain old if/else is not so bad actually. In the end, everybody reading an if/else knows what it means, with a custom function it can be harder to understand sometimes. Regards, Mathias

Thank you again Mathias.  I will look into doing this instead.