'var' and 'volatile' as !const, with invariant-by-default

BCS ao at pathlink.com
Mon Jun 25 12:33:37 PDT 2007


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;

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