any news on const/invariant?

Bill Baxter dnewsgroup at billbaxter.com
Mon Nov 26 17:01:13 PST 2007


Walter Bright wrote:

>> How would I specify that I have a buffer of bytes and I can't change its
>> location but I can change its contents?
> 
> You can't. A good question would be what would be the purpose of such?

I pass you a buffer that I've carefully pre-allocated for you to fill 
in.  You shouldn't reallocate it but you should modify its contents.

Having the function signature say "takes const-pointer-to-mutable-data" 
makes it clear that the function won't be allocating the buffer for you 
or reallocating it out from under you.

Just passing the pointer by value is ok from the caller's perspective 
(you can't change what *my* pointer points to) but from the maintenance 
programmer's perspective there's nothing to prevent accidentally 
re-assigning that mutable pointer to some other buffer inside the 
function and incorrectly making changes there.

--bb (who was just in the process of writing such a modify-this-buffer 
function in D1, sans const)



More information about the Digitalmars-d mailing list