const by default.

kris foo at bar.com
Mon Jul 3 22:24:10 PDT 2006


Chad J wrote:
> So far I'm pretty convinced that some kind of "constness" is a good 
> thing.  I've already run into one area where this would be useful.
> 
> I have one peeve about it though: I really would not like it if the 
> keyword used to represent constness was "const".  I will explain from my 
> newbie-to-Cplusplus background, having come from languages that seem, at 
> least to me, to be much more well thought out than Cplusplus.
> 
> When I see "const" I think of it as a shorthand for "constant", just as 
> "bool" is shorthand for "boolean".  My understanding of something 
> constant is that it does not change... EVER.  Thus it makes no sense to 
> me why something as ephemeral as data passed into a function could be 
> considered constant.  If the constant data leaves the scope of the 
> function, becomes no longer constant, and gets changed, then it isn't 
> really constant to me.
> 
> I do like the mutable word though, as it seems to have an english 
> meaning very close to its programming meaning  :)
> It also seems that if we were to make things immutable by default, we 
> could make an "immutable" keyword.  Sure it's kinda long, but since most 
> things are that by default then we won't be typing it much.  It also 
> seems so much more logically related to the word "mutable".
> 
> Another worry I have is that of being stuck with many instances of 
> "mutable" littering my code, if only because said code happens to modify 
> referenced data a lot.  One way I might see this worry dissappearing if 
> there was a way to blanket stuff into being immutable, in order to avoid 
> repeating the word over and over again.  Sorta like:
> 
> class Foo
> {
>   mutable:
>     int[] data; // this is mutable
>     char[] name; // this too
>     // etc
> }
> 
> or maybe even something as radical as
> 
> mutable
> {
>   // from now on everything works as it used to
> 
>   void func1( char[] string1, char[] string2 )
>   {
>     string1[5] = 'a'; // ok
>     string2[3] = 'b'; // also good
>   }
> 
>   void func2( int[] data )
>   {
>     data[8] = 42; // just dandy.
>   }
> }
> 
> Note:  I am probably so newbish at language design compared to you guys 
> it is not even funny, so maybe the syntax suggestions suck, but please 
> consider my naming plea, at least as a "this experience was unpleasant 
> so please fix it" sorta thing.


hehe -- that would certainly resolve the problem for existing libraries: 
just wrap each entire module in a mutable{} block -- phobos would be 
done in 15 minutes :D

Good idea!



More information about the Digitalmars-d mailing list