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

Russell Lewis webmaster at villagersonline.com
Mon Jun 25 14:57:41 PDT 2007


BCS wrote:
> Reply to Russell,
> 
>> BCS wrote:
>>
>>> 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.
> 
> So the only way to get a non read only reference is with a cast?
> 
> About half the time I use references, it it to get write access. 
> Requiring an cast every time would be a major pain.
> 
> I'd make the '&' as permissive as possible and have the assignment to a 
> non var pointer drop wright access. This sort of going with the idea 
> that code should assert what it will and wont do, but the compiler 
> shouldn't restrict it beyond that.
> 
> Hm. Here their be Interesting thoughts. ;)

Good thoughts.  You can certainly declare the original variable as 
'volatile var', which means that the reference would allow writing. 
However, it would mean that you couldn't cache the value of the variable 
  either in the caller or the callee.

For the case that you are describing (where a caller passes a pointer to 
a non-volatile variable to a callee, which presumably won't use it 
outside the scope of the call), we need a way for the two components to 
agree that:
a) There won't be another thread in the machine, modifying the variable 
while the callee runs
b) The callee won't keep a copy of the reference afterwards.

If we can syntacically enforce both of the above, then both the caller 
and callee can cache the variable (except that the caller must discard 
his copy exactly once-when he calls the function).  I'm still pondering 
how you might do that.



More information about the Digitalmars-d mailing list