'switch' operator improvement

# Eugen.Konkov at aldec.com (18 years ago)

allow RegEx in case

var str= 'a'; switch( str ) { case /a/: alert('a'); break;

case /b/: alert('b'); break; }

# Lars T Hansen (18 years ago)

Neat, though it breaks backward compatibility -- each regexp is converted to string before the comparison, IIRC. (Compatibility may not be a big problem in practice in this case.)

# Dave Herman (18 years ago)

It's clever, but it's a special case that may not abstract very smoothly. For example:

function f(g) { switch (x) { case g(): ... } }

The behavior of my function depends on whether g() returns a RegEx or a non-RegEx. Maybe that's what you want, but it means it's an extra special case that you have to be aware of whenever abstracting a case statement.

# Eugen.Konkov at aldec.com (18 years ago)

I think switch ... case construction must be interpreted as: function f(g) { if( x == g() ) .... // case g(): if( .... // case ... }

----- Original Message ---

# Dave Herman (18 years ago)

But that's not what you proposed, is it? I understood your proposal to mean something more like:

function f(g) { if (let (tmp = g()) // case g(): (tmp is RegEx ? tmp.match(x) : x == tmp)) .... if .... }

# Peter Hall (18 years ago)

Minor nitpick, but that should be "===" not "==".

I really like the idea, but probably it should be done in a way that could be generalised beyond just the RegExp case. And that doesn't seem like something that can be done in the es4 time-frame.

But already you can do something like this:

var str; switch(true){ case /a/.test(str): alert('a'); break; case /b/.test(str): alert('b'); break; }

Not quite as neat or readable, but the same functionality and not overly verbose.

I'd be interested to know how often switch statements are used in the real-world, that could not be trivially replaced with if..else, ie without breaks on every case. Has that been previously measured?

Peter

# Brendan Eich (18 years ago)

On Oct 16, 2007, at 8:19 AM, Dave Herman wrote:

But that's not what you proposed, is it? I understood your proposal to mean something more like:

function f(g) { if (let (tmp = g()) // case g(): (tmp is RegEx ? tmp.match(x) : x == tmp)) .... if .... }

Dave

Right, and that is not only backward incompatible, but user-hostile
unless you type everything. It's perl-ish magic.

I say use an if-else and call match if that is what you want. EIBTI,
the Pythonistas say (Explicit Is Better Than Implicit).

# liorean (18 years ago)

On 16/10/2007, Peter Hall <peter.hall at memorphic.com> wrote:

already you can do something like this:

var str; switch(true){ case /a/.test(str): alert('a'); break; case /b/.test(str): alert('b'); break; }

I can only imagine that solution being preferable to chained if..else-statements in one case, and that would be if you actually used the fall through mechanism. If you don't fall through, it's just bloat without any gain.

# David Teller (18 years ago)
# Eugen.Konkov at aldec.com (18 years ago)

I think this is the best:

  1. Variable in 'switch' is parameter for case
  2. 'case' triggers if result of case expression is 'true' So. switch( aaa ) case <some expression>: case <another expression>: ...

After JS processing if( <modified some expression> ) {

if( <another modified expression> ) {

----- Original Message ---

# Eugen.Konkov at aldec.com (18 years ago)

switch( obj ) { case instanceof String: .... break;

case instanceof Number: .... break;

}