On Sunday, 26 October 2014 at 23:20:21 UTC, Mike wrote:
Language support for this kind of thing would be nice. `@weak` attribute[1] perhaps, or `virtual`/`final` module methods anyone? `final` by default please. :)

[1] - https://gcc.gnu.org/onlinedocs/gcc-4.9.1/gcc/Function-Attributes.html#Function-Attributes

This is turning into a fun little exploration. After reading this issue [1], I discovered that DMD already compiles all the runtime hooks as weak references, so you only need to declare a new `_d_newclass` implementation anywhere in your source code to override `new`.

version(DigitalMars)
{
    extern (C) Object _d_newclass(const ClassInfo ci)
    {
        // add your own `new` implementation here
    }
}

void main(string[] args)
{ }

Compile with:
dmd test.d

No special compiler/linker flags or symbol names. Cool!

LDC has support for decorating functions with LLVM attributes [2] that could potentially include creating `weak` symbols, but LDC will have to decorate `_d_newclass` this way in druntime before overriding with this technique will be possible.

GDC also has infrastructure for supporting such attributes [3], but the `weak` attribute will have to be added to the list, and decorations added to `_d_newclass` in the same way.

Maybe I'll submit some pull requests and see how they're received.

I still don't know how to call the default implementation, using this technique, without copying the source code from druntime. That's not a deal-breaker, but it stinks.

The only way I was able to override `destroy` was to copy the entire object.di from druntime to my project folder and modify the code there. That also stinks.

This little skirmish has opened up a few possibilities for my work. Very interesting.

Mike

[1] - https://github.com/ldc-developers/ldc/issues/405
[2] - https://github.com/redstar/ldc/commit/69f231f48f355b6399d67950f057bea72f0a64c3
[3] - http://wiki.dlang.org/GDC/Using_GDC#Attributes

Reply via email to