const by default.

Rémy Mouëza ray.jay.ay.moueza at DoNtSpAm.gmail.com
Mon Jul 3 13:39:07 PDT 2006


In article <e8bib0$1gn1$6 at digitaldaemon.com>, BCS says...
>
>Hasan Aljudy wrote:
>> Ahem.
>> Excuse me, I'm not very enlightened about this "const" issue, but if 
>> your suggestion implies that people like me who don't care about "const" 
>> will have to add "mutable" qualifiers all over the place, then I'm gonna 
>> have to side against this, with a big NO.
>> 
>> If I've just said something that's too stupid, then please enlighten me. 
>> Thanks.

I also share Hasan's point of view.

>I'm not sure what is being suggested but it's something like this:
>
>Given a function that takes a reference type as an argument. The stuff 
>the reference points to is not a valid target for a write unless you say so.
>
>
>void fn1(char[] c)
>{
>	char i = c[5];	// allowed
>	c[4] = i;	// not allowed
>}
>
>
>void fn2(mutable char[] c)
>{
>	char i = c[5];	// allowed
>	c[4] = i;	// allowed
>}

Why couldn't we do it using the "in" keyword ?

>void function2 ( in char [] c )
>{
>	char i = c [5];	// allowed
>	c [4]  = i ;	// allowed
>}

>Furthermore, unless a variable is mutable, you can't pass it off to a 
>function in a mutable parameter
>
>void fn3(char[] c)
>{
>	fn1(c);	// allowed
>	fn2(c);	// not allowed: c is immutable
>}

Won't we have the same troubles than in C++ ? Mainly copying the immutable
variables to mutable ones that we would pass to the function: 
>void fn3 ( char [] c )
>{
>	fn1 ( c ); // allowed
>	// fn2 ( c );	// not allowed: c is immutable
>       mutable typeof ( c ) d = cast ( mutable ) c.dup ; // Something like this
>       fn2 ( d );
>}

Makes too much copy, memory inefficient for no reason.

>The issue as to the mutability of local variables is another thing all 
>together. I haven't heard any agreement on what is best here. One 
>thought I like is local reference type variables are localy mutable. 
>However if you are passing them to a function they become implicitly 
>immutable unless you say otherwise. (Assume that "@" is the grant 
>mutability operator)
>
>void fn4()
>{
>	char[] c;
>
>	c[5] = '\0';	// allowed
>
>	fn1(c);		// allowed
>	fn2(c);		// not allowed: arg 1 is mutable;
>	fn2(@c)		// allowed
>}
>
We could use the "out" keyword instead of the at sign. That might be more
readable, or understandable.

>In the general case I expect that this change will have little effect on 
>code. In my experience, most pass by reference cases are immutable 
>anyway. The rest should be easy to find and fix as long as DMD gives 
>good error messages.

I don't like the C++ const type system. It creates two parallel universes... uh,
type systems : the const one and the non const one, and we turn to use ugly cast
or copy tricks to fight against that space time disruption. 
I don't want to be stuck in a worm hole again.





More information about the Digitalmars-d mailing list