any news on const/invariant?

Bill Baxter dnewsgroup at billbaxter.com
Wed Nov 28 20:53:43 PST 2007


Walter Bright wrote:
> Bill Baxter wrote:
>> Here's a class that compiles fine and will probably mostly work.
>>
>> class Klass {
>>
>>     this() {
>>         array.length = 10;
>>     }
>>     const int elem(int i) {
>>         return array[i];
>>     }
>>
>>     int[] array;
>> }
>>
>> But someday when someone tries to call elem() with a const Klass it 
>> will fail to compile.
> 
> Yes. So where's the problem? Also, why would one want to return a "const 
>  int"? It doesn't make much sense.

Doh.  Good point.  It doesn't make much sense returning a const int.

I started with the function "const int*elem()", found out that didn't 
compile, and tweaked it so it would compile.  But I forgot to double 
check that the resulting code still made sense.  :-o

--bb



More information about the Digitalmars-d mailing list