'switch' operator improvement

Peter Hall peter.hall at memorphic.com
Tue Oct 16 10:41:15 PDT 2007


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


On 10/16/07, Dave Herman <dherman at ccs.neu.edu> 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
>
> Eugen.Konkov at aldec.com wrote:
> > I think switch ... case construction must be interpreted as:
> > function f(g) {
> >  if( x == g() ) ....     // case g():
> >  if( ....                    //  case ...
> >  }
> >
> > ----- Original Message -----
> > From: "Dave Herman" <dherman at ccs.neu.edu>
> > To: "Lars T Hansen" <lth at acm.org>
> > Cc: <Eugen.Konkov at aldec.com>; <Es4-discuss at mozilla.org>
> > Sent: Tuesday, October 16, 2007 6:04 PM
> > Subject: Re: 'switch' operator improvement
> >
> >
> >> 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.
> >>
> >> Dave
> >>
> >> Lars T Hansen wrote:
> >>> 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.)
> >>>
> >>> --lars
> >>>
> >>> On 10/16/07, Eugen.Konkov at aldec.com <Eugen.Konkov at aldec.com> wrote:
> >>>> allow RegEx in case
> >>>>
> >>>> var str= 'a';
> >>>> switch( str ) {
> >>>>   case /a/:
> >>>>     alert('a');
> >>>>     break;
> >>>>
> >>>>   case /b/:
> >>>>     alert('b');
> >>>>     break;
> >>>>   }
> >>>>
> >>>> _______________________________________________
> >>>> Es4-discuss mailing list
> >>>> Es4-discuss at mozilla.org
> >>>> https://mail.mozilla.org/listinfo/es4-discuss
> >>>>
> >>>>
> >>> _______________________________________________
> >>> Es4-discuss mailing list
> >>> Es4-discuss at mozilla.org
> >>> https://mail.mozilla.org/listinfo/es4-discuss
> > _______________________________________________
> > Es4-discuss mailing list
> > Es4-discuss at mozilla.org
> > https://mail.mozilla.org/listinfo/es4-discuss
>
> _______________________________________________
> Es4-discuss mailing list
> Es4-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es4-discuss
>



More information about the Es4-discuss mailing list