www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - why no '->' operator?

reply Michael Weber <mweber1488 gmail.com> writes:
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.
Jan 31 2007
next sibling parent Brad Roberts <braddr puremagic.com> writes:
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
Jan 31 2007
prev sibling next sibling parent "Lionello Lunesu" <lionello lunesu.remove.com> writes:
"Michael Weber" <mweber1488 gmail.com> wrote in message 
news:eprugq$2t6g$1 digitaldaemon.com...
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 is a pointer to a dynamic array!
 ts.length = 1;
 ts[0].a = 3;

ts[0] *is* an array, not an element. In fact, ts[0] is an array that doesn't exist.
 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;

Here, the array is dereferenced, which is the same as [0], so this line can be written as ts[0][0].a This actually refers to an element, so "a" is known now.
 It would be easier to just have to do ts[0]->a =3; and be done with it.

There's no point for having ->, the compiler can always know whether you mean -> or . In your case, the code just makes no sense, which is why it can't tell :) L.
Jan 31 2007
prev sibling next sibling parent reply Walter Bright <newshound digitalmars.com> writes:
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.

TestStruct[]* ts; declares a pointer to an array of TestStruct. It would be used like: (*ts)[0].a = 3; I'm afraid that the -> won't help here, in C++ this would be declared: TestStruct (*ts)[]; and used like: (*ts)[0].a = 3; as well.
Jan 31 2007
parent Derek Parnell <derek nomail.afraid.org> writes:
On Wed, 31 Jan 2007 22:26:18 -0800, Walter Bright wrote:

 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.

TestStruct[]* ts; declares a pointer to an array of TestStruct. It would be used like: (*ts)[0].a = 3; I'm afraid that the -> won't help here, in C++ this would be declared: TestStruct (*ts)[]; and used like: (*ts)[0].a = 3; as well.

Not only that, but you have to do a little bit first before using that construct. Because 'ts' is a pointer to an array, you have to set it to point to an array first. Then the array has to contain at least one TestStruct before you can set its member fields. struct TestStruct { int a; int b; int c; } void main() { TestStruct[]* ts; // Pointer an array. TestStruct[] x; // Array to point to. ts = &x; // Set ts to point to the array. x.length = 1; // Add a 'blank' TestStruct to the array. (*ts)[0].a = 3; // Now I can set its 'a' member. } -- Derek (skype: derek.j.parnell) Melbourne, Australia "Down with mediocrity!" 1/02/2007 5:34:21 PM
Jan 31 2007
prev sibling parent Whyn Oop <whyn oop.er> writes:
Michael Weber Wrote:

 It would be easier to just have to do ts[0]->a =3; and be done with it.

You mean ts . [0].a = 3 ?
Feb 01 2007