Javari's Reference Immutability

Reiner Pope reiner.pope at gmail.com
Mon Jul 24 23:26:04 PDT 2006


I've read the paper on Javari's reference immutability (thanks to Bruno) 
and it has an interesting idea: a synthesis of dynamic and static 
checking.  This makes it easier to interface const-aware code with 
const-unaware code. It can be demonstrated as follows:

Date a = new Date(); // a mutable date
readonly Date b = a; // A readonly view of a
Date c = (mutable) b; // The compiler bypass static checking, but 
inserts dynamic checks.
a.modify(); // OK
b.modify(); // Compile-time error
c.modify(); // Runtime error


How could you implement the runtime checking? A first thought would be 
to add the following assert to the in contract of every mutating method:

   assert(!isConst);

But that requires that the /class/ knows the const-ness of the reference 
it is accessed through, which I don't think it does.

The other alternative seems to be a much easier one: simply modify the 
vtbl so that all mutating functions point to a single line:

   assert(false, "Trying to modify a class through a readonly view");

The problem with that is that I suspect the vtable is stored with the 
class, not the reference, so modifying the vtable will modify it for all 
other references, even if they aren't readonly.

Clearly, Javari manages such checking, and it claims to manage it in a 
Java-compatible (backwards-compatible) way. How is it done in Javari, 
and how could it be done in D?


Cheers,

Reiner



More information about the Digitalmars-d-learn mailing list