Interesting Research Paper on Constructors in OO Languages

Regan Heath regan at netmail.co.nz
Tue Jul 16 10:17:48 PDT 2013


On Tue, 16 Jul 2013 14:34:59 +0100, Craig Dillabaugh  
<cdillaba at cg.scs.careton.ca> wrote:

> On Tuesday, 16 July 2013 at 09:47:35 UTC, Regan Heath wrote:
>
> clip
>
>>
>> We have class invariants.. these define the things which must be  
>> initialised to reach a valid state.  If we had compiler recognisable  
>> properties as well, then we could have an initialise construct like..
>>
>> class Foo
>> {
>>   string name;
>>   int age;
>>
>>   invariant
>>   {
>>     assert(name != null);
>>     assert(age > 0);
>>   }
>>
>>   property string Name...
>>   property int Age...
>> }
>>
>> void main()
>> {
>>   Foo f = new Foo() {
>>     Name = "test",    // calls property Name setter
>>     Age = 12          // calls property Age setter
>>   };
>> }
>>
>> The compiler could statically verify that the variables tested in the  
>> invariant (name, age) were set (by setter properies) inside the  
>> initialise construct {} following the new Foo().
>>
>> R
>
> How do you envision this working where Name or Age must be set to
> a value not known at compile time?

The idea isn't to run the invariant itself at compile time - as you say, a  
runtime only value may be used.  In fact, in the example above the  
compiler would have to hold off running the invariant until the closing }  
of that initialise statement or it may fail.

The idea was to /use/ the code in the invariant to determine which member  
fields should be set during the initialisation statement and then  
statically verify that a call was made to some member function to set  
them.  The actual values set aren't important, just that some attempt has  
been made to set them.  That's about the limit of what I think you could  
do statically, in the general case.

In some specific cases we could extend this to say that if all the values  
set were evaluable at compile time, then we could actually run the  
invariant using CTFE, perhaps.

R

-- 
Using Opera's revolutionary email client: http://www.opera.com/mail/


More information about the Digitalmars-d mailing list