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

Russell Lewis webmaster at villagersonline.com
Tue Jun 26 10:06:02 PDT 2007


I totally agree, strangely enough.  The syntax I proposed is what (IMHO) 
would work well for a large, formal project, but would be stifling for 
small prototype programs.  I've been pondering if there is some way to 
switch the behavior on and off at will.

How about an attribute which would turn on the invariant-by-default 
functionality?

   uint myVar;  // readonly, since there is no attribute

   invariant-by-default bool myFunc() { ... } // only this func

   uint anotherNormalVar;

   invariant-by-default: // all declarations after this are affected
       uint myROvar;
   var uint myRWvar;

It's a little ugly, but it allows for quick prototype programs to use 
the old syntax, while large projects could just put 
'invariant-by-default' at the top of all of their source files to get 
the nice documentation & optimizing features.

Russ

David B. Held wrote:
> Russell Lewis wrote:
>> [...]
>> * 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.)
>> [...]
> 
> I think a *lot* of programmers would find it onerous to decorate all 
> mutable identifiers.  I think pure value-style programming is best 
> enabled by a fully supported functional environment, which D does not 
> quite have.  It's still fairly awkward to define lambads and currying is 
> not supported to the extent that it is in mainstream FP langs.  Part of 
> D's elegance is that it is not overly verbose, like Java or Ada or other 
> languages.  I also think that invariant-by-default would totally 
> surprise people who were told that D is an "imperative" language that is 
> "a lot like C++".  Remember that a language is as much a cultural 
> phenomenon as a technical one.
> 
> Dave



More information about the Digitalmars-d mailing list