minimal object.d implementation that allows classes

Jean-Louis Leroy jl at leroy.nyc
Thu Dec 7 15:21:08 UTC 2017


On Thursday, 7 December 2017 at 15:09:45 UTC, Jean-Louis Leroy 
wrote:
> On Thursday, 7 December 2017 at 14:59:57 UTC, Steven 
> Schveighoffer wrote:
>> The object is constructed here: [...]
>
> Thanks for the pointers, you've saved me a lot of time :)
>
>> On a side note however, you really shouldn't change data in a 
>> ClassInfo at all, and probably the compiler shouldn't let you!
>
> This experiment is related to an ongoing discussion with 
> Walter, Andrei and Ali on extending D with general mechanisms 
> to better support libraries like openmethods. I will post in 
> Studies soon.

Cool:

import std.stdio;

class Foo {
   abstract void report();
}

class Bar : Foo {
   override void report() { writeln("I'm fine!"); }
}

void main() {
   auto oldPtr = Bar.classinfo.vtbl.ptr;
   Bar.classinfo.vtbl.reserve(1000);
   Bar.classinfo.vtbl.ptr[Bar.classinfo.vtbl.length] = cast(void*) 
0x123456;
   writeln(oldPtr != Bar.classinfo.vtbl.ptr); // true
   *cast(void***) Bar.classinfo.m_init.ptr = 
Bar.classinfo.vtbl.ptr;
   Foo foo = new Bar();
   writeln(oldPtr == *cast(void***)foo); // false
   foo.report(); // I'm fine!
   writeln((*cast(void***)foo)[Bar.classinfo.vtbl.length]); // 
123456
}



More information about the Digitalmars-d mailing list