D's "accessors" are like abusing operator overloads

Steven Schveighoffer schveiguy at yahoo.com
Fri Mar 27 12:21:20 PDT 2009


On Fri, 27 Mar 2009 15:13:16 -0400, Denis Koroskin <2korden at gmail.com>  
wrote:

> On Fri, 27 Mar 2009 21:56:36 +0300, Steven Schveighoffer  
> <schveiguy at yahoo.com> wrote:
>
>> 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;
>>
>
> int x = 5;
> writefln(x.init); // prints 0; how is that possible?

Because init is a static member of the type.  x.init is equivalent to  
int.init.

If you want a different default, I think you do something like:

typedef int myint = 5;

myint x;

writefln(x.init); // prints 5

> It should certainly be either disallowed or yield 5.
>
> int x = 5;
> writefln(typeof(x).init); // 0 as expected
>
> Now this one is fine.

yeah, that works, but I don't like it as much.  I suppose it wouldn't be  
horrible to use typeof(x) everywhere you need static data, but it does  
make things much nicer, especially when you have things like enums or  
constants to just access them.

-Steve



More information about the Digitalmars-d mailing list