D's "accessors" are like abusing operator overloads

Steven Schveighoffer schveiguy at yahoo.com
Fri Mar 27 11:56:36 PDT 2009


On Fri, 27 Mar 2009 13:44:48 -0400, Nick Sabalausky <a at a.a> wrote:

> "Simen Kjaeraas" <simen.kjaras at gmail.com> wrote in message
> news:op.urfzx0ia1hx7vj at biotronic-pc.osir.hihm.no...
>>
>> 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.
>>
>
> That didn't even occur to me. That's certainly a problem too.


Yes, that was my main beef with the compiler when I had to fix it.  I'd  
prefer static member functions not be callable on instances.  On the other  
hand, accessing static members is sometimes convenient:

ReallyReallyLongStructName n;
n = n.init;

Note that even with the poposed fix, this is valid code which makes for  
much confusion:

TimeSpan.fromSeconds = 30;

Which essentially does nothing, but looks like it's setting a static  
property.

I agree with you that the designer of a class should be the one to define  
its interface, not the user.  I've always wished for the ability to  
specify which functions should be properties and which ones should not.

BTW, I think the way C# properties work with chaining is to do this:

// a b c are properties
a = b = c = 42;

translates to:
set_c(42);
set_b(get_c());
set_a(get_b());

-Steve



More information about the Digitalmars-d mailing list