D's "accessors" are like abusing operator overloads

Simen Kjaeraas simen.kjaras at gmail.com
Fri Mar 27 01:45:26 PDT 2009


On Fri, 27 Mar 2009 09:09:27 +0100, Nick Sabalausky <a at a.a> wrote:

> 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!

I feel the problem here is that you can access a type's static members
through an instance of it, not so much the property syntax.

One could argue that auto foo = TimeSpan.seconds = 30; looks weird, but
writing weird code is in no way dependent on property syntax.

--
Simen



More information about the Digitalmars-d mailing list