Comma operator = broken design

Jonathan M Davis jmdavisProg at gmx.com
Wed Dec 7 16:17:04 PST 2011


On Wednesday, December 07, 2011 23:10:53 Timon Gehr wrote:
> On 12/07/2011 07:02 PM, Nick Sabalausky wrote:
> > "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.
> 
> I think Don said that DMD cannot currently inline any delegates. Imho
> the spec should even *require* inlining in the special case that such a
> delegate is immediately executed. (even for debug builds).

lazy doesn't get inlined for the same reason (since it's essentially using a 
delegate), and that potentially has some nasty implications for performance 
with regards to stuff like enforce. In the recent thread on std.regex's 
performance, while enforce was definitely not at the top of the list of 
functions in the profiler (listed descending by running time), it was definitely 
a decent portion of the running time of the program. The cost of enforce 
should be comparable to the cost of assert, but with the issues of inlining 
lazy and delegates, it definitely isn't that efficient. These sorts of things 
need to be inlinable.

- Jonathan M Davis


More information about the Digitalmars-d mailing list