constness for arrays
Derek Parnell
derek at psych.ward
Fri Jul 21 20:03:43 PDT 2006
On Sat, 22 Jul 2006 06:27:24 +1000, Andrew Fedoniouk
<news at terrainformatica.com> wrote:
>
> "Ben Phillips" <Ben_member at pathlink.com> wrote in message
> news:e9rc1u$1g71$1 at digitaldaemon.com...
>> >
>>> operator "=" IS really necessary. As there is no method in D
>>> currently to guard assignment to variable (memory location).
>>> Again without it good chunk of RAII methods and smart pointers
>>> are not implementable in D.
>>>
>>
>> It is impossible to allow operator "=" to be overloaded without totally
>> killing
>> the way D works because D uses references.
>> Example:
>> ClassA a = new ClassA();
>> ClassA b = a; // b now refers to a
>> b.mutate(); // both 'b' and 'a' are changed since they refer to the same
>> object
>>
But some of the side effects of a new operator ':=' would be that it could
be used with value types and reference types alike and mean that the
information contained in the right-hand side member is copy to the
left-hand side member. It would remove the need for ".dup" for example.
char[] a;
a := toString(4);
> method is not an option at all.
> operator= is a guard of memory loacation and method, well, is method.
>
> struct guard {
> int v;
> void opAssign(int nv) { alarm("value 'v' is about to change"); v =
> v; }
> }
>
> guard gv;
> gv = 12;
>
> As you may see operator= guards memory location allowing you to intercept
> all assignments into the variable. Too many things (RAII, smart pointers)
> were built
> around this in C++.
>
> Method of the struct will not help you here in principle.
struct guard {
private int _v;
void v(int nv) { alarm("value 'v' is about to change"); _v = v; }
}
guard gv;
gv.v = 12 ;
--
Derek Parnell
Melbourne, Australia
More information about the Digitalmars-d
mailing list