suggestion: read-only array-reference

Johan Granberg lijat.meREM at OVEgmail.com
Thu Jul 20 06:34:44 PDT 2006


Dave wrote:
>> 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.

I agree with you about the library beeing incorrectly written. But 
notice this line in my reply.

 >> or internally in your own code base where you can use this as a 
shortcut)

the case when you have a class like this

class Foo
{
	const char[] name;
	void setName(const char[] c){(cast(char[])name)[]=c;}
}

this could bee achieved by using properties but I think this should bee 
allowed.



More information about the Digitalmars-d mailing list