any news on const/invariant?

Bill Baxter dnewsgroup at billbaxter.com
Tue Nov 27 00:49:06 PST 2007


Derek Parnell wrote:
> On Mon, 26 Nov 2007 23:23:41 -0800, Nathan Reed wrote:
> 
>> Janice Caron wrote:
>>> I guess it could change its own local copy of the pointer, but that
>>> would just be a bug local to the function.
>> One which, I would think, should be caught by the type system.  However, 
>> if there's no 'final', I guess this can't be done.
>>
>> On the other hand, maybe the compiler should just emit a warning when a 
>> passed-by-value parameter is written to.  If you really do want to 
>> change your local copy of a parameter you can always copy it to a variable.
>>
> 
> I'm talking about having the compiler help catch coding mistakes at compile
> time. In this case, the compiler can only know that it is a mistake to
> (attempt to) modify the pointer if the coder tells it that the design
> forbids that behaviour. Therefore we need to have form of syntax to help
> the compiler help the coder.

If you're worried about the implementer doing the wrong thing then you 
could argue that the same applies to all arguments.

void foo(int x) {
    x = 10; /* oops! did I mean to do that? Or was I hoping the user 
would see it? */
}

But there is a difference in that if you're really worried about that 
possibility then you can make it foo(const int), but without head const 
you can't do the same thing for just the pointer.

But is it important enough to justify the extra complication?  We get by 
in D1.0 without const at all, after all.  Java gets by without it too. 
I'm ready to accept a const that can't express every nuance if it 
enables greater simplicity.

--bb



More information about the Digitalmars-d mailing list