D's "accessors" are like abusing operator overloads

Nick Sabalausky a at a.a
Fri Mar 27 01:09:27 PDT 2009


I don't mean to put words in the Tango crew's mouths, but IMO this piece 
from 0.99.8's changelog is a great demonstration of what's wrong with D's 
sloppy "accessor" syntax:

-------------
tango.time

* TimeSpan.seconds(ulong), etc. is now replaced by 
TimeSpan.fromSeconds(ulong), etc. The original form causes problems when 
people write code like:

auto ts = TimeSpan.seconds(60);
ts.seconds = 30; // you would think this would assign 30 seconds to ts, but 
what it does is create a
                 // temporary and throw it away.
-------------

The fact that D lets you freely switch between "foo(x)" and "foo = x" (at 
least when you're not using a return value from foo) implies that passing 
one argument to a function is, in the general case, conceptually related to 
assigning a value. And that is an absurdity and abuse of syntax on the same 
level as pretending that string concatenation is an "addition" or that 
outputting is a "<<", etc.

And this "solution" that Tango was forced to use as a result of that kludge 
doesn't totally solve the problem since it leaves symantic gibberish like 
this as perfectly-compileable:

ts.fromSeconds = 30; // WTF is that supposed to mean?!? But it compiles 
anyway! 





More information about the Digitalmars-d mailing list