const?? When and why? This is ugly!
Walter Bright
newshound1 at digitalmars.com
Sat Mar 7 19:36:57 PST 2009
Derek Parnell wrote:
> On Sat, 07 Mar 2009 14:43:50 -0800, Walter Bright wrote:
>
>> int foo(const(char)[] s)
>>
>> what if foo() keeps a private reference to s (which it might if it does
>> lazy evaluation)? Now I, as a caller, mutate s[] and muck up foo. So, to
>> fix it, I do:
>>
>> foo(s.dup); // defensive copy in case foo keeps a reference to s
>
> In foo's defence, if it takes a private reference, then it should also take
> a copy.
Yup, and as I said, an extra copy "just in case".
> In fact, should it be allowed to take a private reference of data
> which might be modified after it returns?
>
Instead of adding more complexity to const so it acts more like
immutable, why not just use immutable <g> ?
More information about the Digitalmars-d
mailing list