'var' and 'volatile' as !const, with invariant-by-default
Russell Lewis
webmaster at villagersonline.com
Mon Jun 25 12:52:08 PDT 2007
BCS wrote:
> Reply to Russell,
>
>> So I understand what Walter is going for with the whole
>> const/invariant/final thing, but it seems that the syntax is causing a
>> lot of confusion. (I know it isn't clear to me!) People have been
>> arguing back and forth about const-by-default, and here's my version:
>>
>> * Invarant-by-default. That is, an unadorned variable (whether it is
>> a global variable, function local, or parameter) is always
>> invariant.
>> * Use the keyword 'var' to represent a variable which can be modified.
>> * Use the keyword 'volatile' to indicate that there are aliases of the
>> variable, so it is not safe to cache the value of the variable in a
>> register.
>> * 'volatile' is illegal on value-types. (Only allowed on pointers,
>> arrays, class references, etc.)
>
> unless all var value type are volatile this has a problem.
>
> var int i = 3;
> var volatile int* j = &i;
This line is not legal, in my original rules, because when you take a
pointer to a 'var' variable, you get a 'volatile' pointer. You can't
assign that to a 'volatile var' variable without an explicit cast.
I'm open to the idea that you could allow 'volatile' on value-types, but
I'm skeptical as to its value. But don't let my skepticism shoot down
the whole idea. :)
As for your example with the variables 'j' and 'jp' below, that would be
exactly how my proposal would work (if 'volatile' were allowed on
value-types). In my original post, I said that if you take a pointer to
a 'volatile var' you get a 'volatile var'.
So you & I are on the same wavelength. :)
> i=5; if(*j)... // ok j volatile
>
> *j = 3; if(i)... // oops i is aliased
>
> how about allow volatile on value types, and requiter it to get a non
> read only reference to it
>
> var int i = 3;
> var volatile int j = 3;
>
> auto ip = &i; // read only reference to i.
> auto jp = &j; // read/write reference to j.
>
> auto jp = ip; // invalid
>
>
>> Thoughts?
>>
>> Russ
>>
>
>
More information about the Digitalmars-d
mailing list