Re: Another switch
Also only works when you're switching on something with a meaningful conversion to string. On 21 February 2014 07:03, Mathias Bynens mathi...@opera.com wrote: On 20 Feb 2014, at 21:20, Eric Elliott e...@ericleads.com wrote: Object literals are already a great alternative to switch in JS: var cases = { val1: function () {}, val2: function () {} }; cases[val](); In that case, you’d need a `hasOwnProperty` check to make sure you’re not trying to call `__proto__` or `toString`, etc. See https://github.com/rwaldron/idiomatic.js/#misc for a more complete example. ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
Re: Another switch
On Fri, Feb 21, 2014 at 7:55 AM, Nick Krempel ndkrem...@google.com wrote: Also only works when you're switching on something with a meaningful conversion to string. On 20 Feb 2014, at 21:20, Eric Elliott e...@ericleads.com wrote: Object literals are already a great alternative to switch in JS: var cases = { val1: function () {}, val2: function () {} }; cases[val](); Right, this wouldn't work if the case wanted object references, but it would work nicely with Symbols. Rick ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
Re: Another switch
In practice, I find that everything converts nicely to a string when you precede it with a ternary assignment. I also find that when you do that, it's pretty trivial to control what those strings are, which makes `hasOwnProperty` superfluous. I haven't used a switch in JavaScript for quite a few years now, and I don't miss it at all. - Eric Author, Programming JavaScript Applications (O'Reilly) http://ericleads.com/ On Fri, Feb 21, 2014 at 10:26 AM, Rick Waldron waldron.r...@gmail.comwrote: On Fri, Feb 21, 2014 at 7:55 AM, Nick Krempel ndkrem...@google.comwrote: Also only works when you're switching on something with a meaningful conversion to string. On 20 Feb 2014, at 21:20, Eric Elliott e...@ericleads.com wrote: Object literals are already a great alternative to switch in JS: var cases = { val1: function () {}, val2: function () {} }; cases[val](); Right, this wouldn't work if the case wanted object references, but it would work nicely with Symbols. Rick ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
Re: Another switch
In the ES6 world, you should probably set up a Map for your switch statement; that would allow you to easily use non-string cases. --scott ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
Re: Another switch
Object literals are already a great alternative to switch in JS: var cases = { val1: function () {}, val2: function () {} }; cases[val](); Fall through is more trouble than it's worth, IMO. On Feb 17, 2014 1:44 PM, Giacomo Cau cau.giacomo...@tiscali.it wrote: -Messaggio originale- From: Brendan Eich Sent: Wednesday, February 12, 2014 2:00 AM To: Nathan Wall Cc: Giacomo Cau ; es-discuss@mozilla.org Subject: Re: Another switch Definitely good to see new languages being designed and implemented. JS is not going to break compatibility on the old fall-through behavior of switch, inherited from Java from C++ from C. All the C-like languages copy this flaw, because to do otherwise with the same keyword would be worse (confused users cross-training and -coding among languages would want our scalps), and IMHO using novel reserved words would be hardly better. /be Nathan Wall mailto:nathan.w...@live.com February 11, 2014 at 3:21 PM Hi Giacomo, Not sure whether this will be of interest to you, but I have been working on a JS-derived language called Proto (still highly experimental) which has a switch statement that works exactly as you described: https://github.com/Nathan-Wall/proto/blob/master/docs/control/switch.md Perhaps you will at least find it interesting. :) Nathan yes, so great it would be a pleasure to contribute :) the proposal doesn't want, by no means, to break the compability with the present syntax and/or semantics of the switch. At most, should be considered as an extension of current syntax with a consequential new semantics. the swith, as it is known, should anyhow be written as switch (...) { case ...: ...; break; case ...: ...; break; case ...: ...; case ...: ...; break; otherwise: ... } but, it could be handy to write the same thing with a slightly different syntax: switch (...) break { case ...: ...; case ...: ...; case ...: ...; continue; case ...: ...; otherwise: ... } Some break less, no new keyword, no incompatibility with past. Who shall use it, will know from the start that here, the continue will not lead him at the beginning of the first for or while that englobe the switch, but only at the following case. Then, willingly, we could also think that normal switch be desugared in switch (...) continue { case ...: ...; break; case ...: ...; break; case ...: ...; case ...: ...; break; otherwise: ... } with break and continue (except that between ')' and '{' ) that, obviously, continue as before. This isn't a must, it's just for my pleasure in finding regularity. At last, if we were to look for the burden of a new keyword, even better select (...) { case ...: ...; case ...: ...; case ...: ...; continue; case ...: ...; otherwise: ... } but I realize this could be a break point of the existing code and, onestly speaking, it is beyond my ability to evaluate. Anyhow, thanks for your attention. bye Giacomo Cau ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
Re: Another switch
On 20 Feb 2014, at 21:20, Eric Elliott e...@ericleads.com wrote: Object literals are already a great alternative to switch in JS: var cases = { val1: function () {}, val2: function () {} }; cases[val](); In that case, you’d need a `hasOwnProperty` check to make sure you’re not trying to call `__proto__` or `toString`, etc. See https://github.com/rwaldron/idiomatic.js/#misc for a more complete example. ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
Re: Another switch
-Messaggio originale- From: Brendan Eich Sent: Wednesday, February 12, 2014 2:00 AM To: Nathan Wall Cc: Giacomo Cau ; es-discuss@mozilla.org Subject: Re: Another switch Definitely good to see new languages being designed and implemented. JS is not going to break compatibility on the old fall-through behavior of switch, inherited from Java from C++ from C. All the C-like languages copy this flaw, because to do otherwise with the same keyword would be worse (confused users cross-training and -coding among languages would want our scalps), and IMHO using novel reserved words would be hardly better. /be Nathan Wall mailto:nathan.w...@live.com February 11, 2014 at 3:21 PM Hi Giacomo, Not sure whether this will be of interest to you, but I have been working on a JS-derived language called Proto (still highly experimental) which has a switch statement that works exactly as you described: https://github.com/Nathan-Wall/proto/blob/master/docs/control/switch.md Perhaps you will at least find it interesting. :) Nathan yes, so great it would be a pleasure to contribute :) the proposal doesn't want, by no means, to break the compability with the present syntax and/or semantics of the switch. At most, should be considered as an extension of current syntax with a consequential new semantics. the swith, as it is known, should anyhow be written as switch (...) { case ...: ...; break; case ...: ...; break; case ...: ...; case ...: ...; break; otherwise: ... } but, it could be handy to write the same thing with a slightly different syntax: switch (...) break { case ...: ...; case ...: ...; case ...: ...; continue; case ...: ...; otherwise: ... } Some break less, no new keyword, no incompatibility with past. Who shall use it, will know from the start that here, the continue will not lead him at the beginning of the first for or while that englobe the switch, but only at the following case. Then, willingly, we could also think that normal switch be desugared in switch (...) continue { case ...: ...; break; case ...: ...; break; case ...: ...; case ...: ...; break; otherwise: ... } with break and continue (except that between ')' and '{' ) that, obviously, continue as before. This isn't a must, it's just for my pleasure in finding regularity. At last, if we were to look for the burden of a new keyword, even better select (...) { case ...: ...; case ...: ...; case ...: ...; continue; case ...: ...; otherwise: ... } but I realize this could be a break point of the existing code and, onestly speaking, it is beyond my ability to evaluate. Anyhow, thanks for your attention. bye Giacomo Cau ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
RE: Another switch
Hi Giacomo, Not sure whether this will be of interest to you, but I have been working on a JS-derived language called Proto (still highly experimental) which has a switch statement that works exactly as you described: https://github.com/Nathan-Wall/proto/blob/master/docs/control/switch.md Perhaps you will at least find it interesting. :) Nathan From: cau.giacomo...@tiscali.it To: es-discuss@mozilla.org Subject: Another switch Date: Sun, 9 Feb 2014 10:41:40 +0100 Hello to all I wish to submit a little proposal. Today the switch statement has an explicit break at the end of the statement and an implicit continue to the next case but this break is very boring and error prone. Wouldn’t it be possible to think a switch that has an explicit continue to the next case and an implicit break at the end of the statement? This is the hypothetical new statement syntax with a new keyword: hctiws ( ... ) { ... } select ( ... ) { ... } or without a new keyword: switch ( ... ) break { ... } but with the current switch equals to switch ( ... ) continue { ... } bye Giacomo Cau ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
Re: Another switch
Definitely good to see new languages being designed and implemented. JS is not going to break compatibility on the old fall-through behavior of switch, inherited from Java from C++ from C. All the C-like languages copy this flaw, because to do otherwise with the same keyword would be worse (confused users cross-training and -coding among languages would want our scalps), and IMHO using novel reserved words would be hardly better. /be Nathan Wall mailto:nathan.w...@live.com February 11, 2014 at 3:21 PM Hi Giacomo, Not sure whether this will be of interest to you, but I have been working on a JS-derived language called Proto (still highly experimental) which has a switch statement that works exactly as you described: https://github.com/Nathan-Wall/proto/blob/master/docs/control/switch.md Perhaps you will at least find it interesting. :) Nathan ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss Giacomo Cau mailto:cau.giacomo...@tiscali.it February 9, 2014 at 1:41 AM Hello to all I wish to submit a little proposal. Today the switch statement has an explicit break at the end of the statement and an implicit continue to the next case but this break is very boring and error prone. Wouldn’t it be possible to think a switch that has an explicit continue to the next case and an implicit break at the end of the statement? This is the hypothetical new statement syntax with a new keyword: hctiws ( ... ) { ... } select ( ... ) { ... } or without a new keyword: switch ( ... ) break { ... } but with the current switch equals to switch ( ... ) continue { ... } bye Giacomo Cau ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
Re: Another switch
Hi Nathan, Since the `switch/case` construct in Proto runs differently from the usual way, it should be spelt differently to avoid the confusion that will inevitably occur when switching between languages. For instance, Perl names it `given/when` [1], and SQL uses `case/when`. —Claude [1] http://perldoc.perl.org/perlsyn.html#Switch-Statements Le 12 févr. 2014 à 00:21, Nathan Wall nathan.w...@live.com a écrit : Hi Giacomo, Not sure whether this will be of interest to you, but I have been working on a JS-derived language called Proto (still highly experimental) which has a switch statement that works exactly as you described: https://github.com/Nathan-Wall/proto/blob/master/docs/control/switch.md Perhaps you will at least find it interesting. :) Nathan From: cau.giacomo...@tiscali.it To: es-discuss@mozilla.org Subject: Another switch Date: Sun, 9 Feb 2014 10:41:40 +0100 Hello to all I wish to submit a little proposal. Today the switch statement has an explicit break at the end of the statement and an implicit continue to the next case but this break is very boring and error prone. Wouldn’t it be possible to think a switch that has an explicit continue to the next case and an implicit break at the end of the statement? This is the hypothetical new statement syntax with a new keyword: hctiws ( ... ) { ... } select ( ... ) { ... } or without a new keyword: switch ( ... ) break { ... } but with the current switch equals to switch ( ... ) continue { ... } bye Giacomo Cau ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
Another switch
Hello to all I wish to submit a little proposal. Today the switch statement has an explicit break at the end of the statement and an implicit continue to the next case but this break is very boring and error prone. Wouldn’t it be possible to think a switch that has an explicit continue to the next case and an implicit break at the end of the statement? This is the hypothetical new statement syntax with a new keyword: hctiws ( ... ) { ... } select ( ... ) { ... } or without a new keyword: switch ( ... ) break { ... } but with the current switch equals to switch ( ... ) continue { ... } bye Giacomo Cau___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
RE: Subject: Another switch
I'm new too, but don't think that's exactly how things get proposed here :) Spec changes don't start with solutions - they start with existing problems you're trying to solve. For example. In ES6 I might do ```js var res = ({ hello : () = 12, /* no fallback */ world : () = 323, /* no fallback */ ... })[ myWord] ``` Or just an if, or other things. The cost of adding a keyword into the language is big (you're breaking any code that uses that keyword as an identifier which is a lot). My suggestion is that you make a static analysis tool that warns people about switch fallthrough and then advocate it. Tools like jshint/jslint will warn you about it, using such tools can (and should IMO) be a part of your build and release process. Thanks, Benjamin ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss