why no '->' operator?

Brad Roberts braddr at puremagic.com
Wed Jan 31 21:58:58 PST 2007


Michael Weber wrote:
> I have used D for a few months now and its the best thing I have found since C++. However, there are a number of things that bother me about D. I am not getting into all of them just my qualms about the arrow operator. I, for one, do not see a reason why it was not included. Here is a reason why it should be included:
> 
> struct TestStruct {
>         int a,b;
> }
> TestStruct[]* ts;
> ts.length = 1;
> ts[0].a = 3;
> 
> The dot operator is supposed to resolve the structure pointer and assign a to be three but this wont compile because the compiler thinks that a is a property of arrays and not a member of the structure. Instead I have to do this:
> 
> (*ts[0]).a = 3;
> 
> It would be easier to just have to do ts[0]->a =3; and be done with it.

Any particular reason you didn't use just TestStruct[] ts?  A dynamic 
array of pointers is a tad on the unusual side (though certainly not 
wrong).  Assuming, if you really intend to have a dynamic array of 
pointers, you'll need to add an allocation step before the last line, 
something akin to:

     ts.length = 1;
     ts[0] = new TestStruct;
     (*ts[0]).a = 3;

That said, I'm inclined to think the need for the explicit * dereference 
is strictly a bug.  D is supposed to be able to automatically know that 
. is a pointer dereference when a pointer is involved and a reference 
dereference when it's a reference.

Once someone corroborates my suspicion, one of us will need to file the 
bug report.

Later,
Brad





More information about the Digitalmars-d mailing list