Const sucks
Jason House
jason.james.house at gmail.com
Mon Sep 10 19:31:53 PDT 2007
Walter Bright 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?
>
> That's sort of what the often proposed 'unique' type qualifier does. The
> problem is, there's just no reasonable way to implement it in a
> *checkable* manner. And if it ain't checkable, it's about as useful as
> painting a black stripe across your lap when driving instead of wearing
> a seatbelt.
Except for the most pathological cases, I believe there are some
checkable ways to do this.
Consider a piece of code that uniquely has write privilege to a variable
(and is only ever called from a specific thread). In that case, it can
give out scoped invariant access when calling functions within its
thread, or const access to anything.
Violation of the unique write privilege should be easy enough to
check/enforce. For example, calling a function (or passing a delegate)
with non-const access.
Of course, this all relies on avoiding multi-threaded calls to a
function. While I haven't thought it all out, I think the answer may
lie with thread-local storage.
More information about the Digitalmars-d
mailing list