Make a variable single-assignment?
Timon Gehr
timon.gehr at gmx.ch
Wed Nov 30 15:32:53 PST 2011
On 12/01/2011 12:08 AM, Stewart Gordon wrote:
> On 21/11/2011 20:06, Jesse Phillips wrote:
>> What you are describing is Head Const, and is not available.
>>
>> http://www.d-programming-language.org/const-faq.html#head-const
>>
>> It will not be added as it doesn't provide any guarantees about the
>> code that is useful
>> to the compiler. It can't be added to the existing system without
>> complicating the type
>> system even more, which outweighs the benefits.
> <snip>
>
> Guarantees about the code don't need to be useful to the compiler - they
> can be just useful to the programmer. After all, this is the main point
> of DbC.
>
> And it doesn't need to be a full-fledged head const. At the simplest, a
> single-assignment variable could just be an rvalue - something of which
> the address cannot be taken and so the absence of head const becomes
> irrelevant.
>
> That said, it isn't much complexity to allow the address to be taken of
> such a thing
>
> final T data;
> auto ptr = &data;
>
> with the following rules:
> - if T is a value type, immutable(something)[] or immutable(something)*,
> then &data is an immutable(T)*
> - otherwise, &data is a const(T)*.
>
> Once upon a time there was const/final/invariant. What exactly did final
> do back then?
>
> Stewart.
Here is some documentation from that time:
http://www.digitalmars.com/d/final-const-invariant.html
It did indeed mean head const, like in Java. I wonder why it was removed
from the design though, it gives extra expressiveness at no cost. (maybe
because then people will start pushing for readonly (tail const)? :))
More information about the Digitalmars-d-learn
mailing list