suggestion: read-only array-reference

Dave Dave_member at pathlink.com
Thu Jul 20 05:39:04 PDT 2006


Johan Granberg wrote:
> Hasan Aljudy wrote:
>> Additionally, we can use the same rational for builtins that Walter 
>> used for other things: having this built-in means that the compiler 
>> knows about the read-only array idiom, and can provide meaningful 
>> error messeges and prevent any possible attempt to break the contract 
>> (using asm or whatever pointer tricks available).
> 

Subversion by asm would probably be impossible to prevent. But that's 
Ok. Even if you could subvert through pointers and casting tricks the 
compiler could enforce the normal cases and the rest could be covered 
with something in the spec like "const really means 'constant' in D. 
Subversion of const is disallowed and the results are undefined."

> While I'm all for a built in const I disagree with the last paragraph. I 
> don't want the compiler to try to prevent me subverting the protection 
> by using casts or pointer tricks (c++ had const cast for a reason). I 
> have used some c++ libraries where some values where const when not 
> strictly needed, and I was able to achieve the desired behavior by the 
> use of a cast. (This is of course unsafe and should never bee used in 
> library code, just in quick and dirty applications or internally in your 
> own code base where you can use this as a shortcut)

And I disagree with that <g> If const was not strictly needed (or could 
not easily be subverted w/o asm as you can w/ C++) then the C++ library 
you mention should not have used it. With some sort of "true const" D 
libraries would be written differently.



More information about the Digitalmars-d mailing list