Const sucks
Bruce Adams
tortoise_74 at yahoo.couknowthedrill.com
Mon Sep 10 17:17:27 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.
I see a least two ways of interpreting it at run-time.
Acquire a lock before entering the function (or at least the parts where the value is modified) or in an STM atomic transaction roll-back and try again if the value is modified.
Being run-time only I suspect a function or template and lazy evaluation might suffice.
At compile time you could use it as a hint for 'linting' race conditions out of your code. That is a useful instrumentation to have.
(cyclist wearing a flourescent band as opposed to a black stripe)
These are just suggestions I wouldn't advocate anything without more thought. As I said on the next branch over this is off topic for a discussion about const.
Regards,
Bruce.
More information about the Digitalmars-d
mailing list