Overloading the assignment operator, right now!
Derek Parnell
derek at nomail.afraid.org
Fri Sep 15 00:56:56 PDT 2006
On Fri, 15 Sep 2006 09:43:07 +0200, Oskar Linde wrote:
> Derek Parnell wrote:
>> On Thu, 14 Sep 2006 17:30:29 +0300, Georg Wrede wrote:
>>
>>> Overloading of the assignment operator is not allowed in D.
>>>
>>> But no problem, we can do it right now, in spite of it.
>> ...
>>
>> I'm unhappy that D doesn't have a copy operator. If it did, I'd like to
>> overload that!
>
> But in a way it does have one such operator. I am thinking about []=.
>
> struct Matrix {
> T* ptr;
> size_t w,h;
> ...
> Matrix opSliceAssign(Matrix b) {...}
> }
>
> Matrix a,b;
> ...
>
> a=b; // Makes a refer to the same memory as b
> a[]=b; // Copies the contents of b to a
This is still a workaround though as it doesn't really work for arrays
(it's just a shallow copy and you can't overload it), and doesn't work for
scalars (bad syntax).
However, a new operator that can be used with all types and can be
overloaded for structs, and classes would be a much better alternative,
especially with generic coding (templates/mixins) in mind.
Hopefully Walter will 'see the light' and schedule this useful concept into
v2 ;-)
--
Derek
(skype: derek.j.parnell)
Melbourne, Australia
"Down with mediocrity!"
15/09/2006 5:50:16 PM
More information about the Digitalmars-d
mailing list