Will D ever get a reference type?
Bill Baxter
dnewsgroup at billbaxter.com
Sat Jul 28 13:00:47 PDT 2007
> Bill Baxter Wrote:
>
>> Will D ever get a reference type?
>>
>> I don't think pointers are really sufficient.
>> The handy 'foo.bar' == '(*foo).bar' attribute access is nice, but it
>> doesn't allow you to really manipulate values and pointers to values
>> interchangeably.
>>
>> For instance, if I have an int pointer, x, I can't do (12 + x * 2) and
>> get integer math. I get pointer math (if not an error).
Nick Sabalausky wrote:
> You know you can just dereference the pointer like (12 + *x * 2),
right? (Sorry if you already did). Or are you saying you don't like
needing to dereference it like that?
>
The latter. D does go out of its way to try to remove the need for
reference types ('.' can dereference a pointer, and opIndexAssign
handles the a[i]=5), but there are still things you can't do. Like
implement op-increment indexing operators for a class/struct:
a[i]+=5;
a[i]-=5;
a[i]*=5;
a[i]/=5;
a[i]++;
a[i]--;
...
Or pass the result of a.opIndex(i) to a function that modifies the argument:
setValue(a[i]) //calling void setValue(ref int i)
This means it's not possible to create an array-like class that's truly
interchangeable with the built-in arrays.
It also affects properties. If there were reference types then it would
be possible (though some would argue not advisable) to have the
op-increment work for property methods:
a.x += 5;
where x is a member function like:
ref int x() { return my_x; }
So really what I was wondering is if Walter considers D's current
pointers plus reference parameters to be enough.
Are there any other things you can't currently do in D that reference
types would allow?
--bb
More information about the Digitalmars-d
mailing list