Const sucks

Russell Lewis webmaster at villagersonline.com
Mon Sep 10 15:29:19 PDT 2007


Gregor Richards wrote:
> Russell Lewis wrote:
>> Walter Bright wrote:
>>> What we are trying to achieve:
>>>
>>> a) utility for functional programming
>>> b) better modularity by self-documenting interfaces better
>>> c) be able to treat reference types as if they were value types (i.e. 
>>> strings should behave to the user like value types, even though they 
>>> are references)
>>
>> Is there a way to express (for a pointer passed as a function 
>> parameter) "the callee is allowed to modify this object through the 
>> pointer, but the caller ensures that no other code will be modifying 
>> it at the same time"?   Sort of a "you have exclusive write access" 
>> modifier?  Is that a common enough case to even support?
>>
>> Russ
> 
> It would probably be easy enough to add such a type modifier, but 
> literally impossible to to hold that restriction, so I don't think it's 
> worth it. There's no magic-bullet for concurrency.

I agree that there's no magic-bullet for concurrency, but there *are* 
times where it could be enforced:

1) The caller's variable is a local variable, and either there have been 
no pointers to the local (including frame pointers created by delegate 
literals), or else all of those locals were passed to external code as 
"const."

2) The caller already holds the variable in "exclusive" mode.

(I haven't figured out any way, yet, to make it work for globals.)

Whether it's worth the trouble is a whole 'nother question, of course.



More information about the Digitalmars-d mailing list