constness for arrays

Bruno Medeiros brunodomedeirosATgmail at SPAM.com
Thu Jul 20 06:27:45 PDT 2006


xs0 wrote:
> Andrew Fedoniouk wrote:
>> b) does not solve compile verification of readonlyness and
> 
> I said so myself :P But, the question is whether compile-time 
> verification is better or not. In some cases it definitely isn't;
> 
> int[] cowFoo(int[] a) { if (whatever) { a=a.dup; a[0] = 5; } }
> int[] cowBar(int[] a) { if (something) { a=a.dup; a[1] = 10; } }
> 
> int[] result=cowFoo(cowBar(whatever));
> 
> How can a compile-time check ever help you avoid the (unnecessary) 
> second .dup when both funcs decide to modify the data?
> 

For that case (how to avoid the unnecessary dups):

   int[] cowFoo(int[] a) { if (whatever) { a[0] = 5; return a; } }
   int[] cowBar(int[] a) { if (something) { a[1] = 10; return a; } }

   int[] result=cowFoo(cowBar(someintar));

What's the a.dup for? Do you realize that if the parameter (a) is 
non-const then that means the function is allowed to change it? Perhaps 
you meant a different use case?


-- 
Bruno Medeiros - CS/E student
http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D



More information about the Digitalmars-d mailing list