Thursday, February 23, 2012

???? (A new ternary style operator)

I saw a silly post ending with "????" and my brain pounced on the multiple question marks, at the same time wondering when three was enough or if four is warranted. Perhaps because of javascript can use === to test equality I thought the ???? could also be an operator, riffing off the ternary style of compressing if/else into one line.

Well, ???? Could be a compressed switch/case statement!

Ternary:
if (a > b) {
    result = x;
}
else {
    result = y;
}
 
becomes...
 
result = a > b ? x : y; 


????
switch (input) {
    case: "compare to one possible input"
    "do something"
    break;

    case: "some other possible input"
    "do something else"
    break;
 
    "etc" 
 
Using similar characters as the ternary...
 
result = input ? comparison 1 : output ? comparison 2 : output ? comparison 3 : output ... etc
 
Anyways, just having some fun, right????  
 
** edit: it could be called multiplus