Comma operator = broken design
Nick Sabalausky
a at a.a
Wed Dec 7 10:02:17 PST 2011
"Adam Ruppe" <destructionator at gmail.com> wrote in message
news:jbo8rr$rhh$1 at digitalmars.com...
> Alex Rønne Petersen Wrote:
>> I really do not see the value in allowing such syntax in the first
>> place. I've been told that one argument was that generated code might
>> use it, but I have no idea why it would be needed.
>
>
> Aside from the compiler's implementation, one possible use
> is something I ended up doing in Javascript recently.
>
> I have a thing that takes an attribute and pastes it into a code
> string to check it.
>
> given validate="this.value.length > 3"
>
> it writes:
>
> if(!(this.value.length > 3))
> return false; // failed validation
>
> since the given string is inside an if statement, you can't put
> a semicolon in there.
>
>
> So, if you have a check more complex than returning a boolean
> and want to stuff it all in that string (so functions are out), the
> comma lets you do it:
>
> validate="do something, true"
>
>
>
> This is pretty ugly style that I think I've only ever done in D inside
> a for loop... but the point is sometimes something comes up, and
> it's nice to have another option available, even if it is ugly.
Don't know about JS, but D can solve the same problem with anon delegates,
which is less obscure anyway.
More information about the Digitalmars-d
mailing list