I have made a discovery

w0rp via Digitalmars-d digitalmars-d at puremagic.com
Sat Apr 18 08:24:25 PDT 2015


The following code almost compiles.

--------
import core.stdc.stdlib;

class Foo : Exception {
     @nogc pure nothrow @safe
     this(string msg, string file = __FILE__, size_t line = 
__LINE__, Throwable next = null) {
         super(msg, file, line, next);
     }

     @nogc
     new(size_t size) {
         return malloc(size);
     }
}

@nogc
void main() {
     throw new Foo("Oh no!");
}
--------

That's right. An unofficially deprecated feature of the language 
and a newer feature of the language coming together in an 
interesting way. The only thing stopping this code from actually 
working is a trivial change to druntime to mark the Throwable, 
Exception, and Error constructors as @nogc, which I just created 
a pull request for directly through GitHub.

https://github.com/D-Programming-Language/druntime/pull/1223

Now imagine that instead of just a malloc which leaks memory like 
the above, other allocation schemes are used here instead. 
Consider also the coming addition to the language for class 
reference counting methods opAddRef and opRelease. Then let your 
imagination run wild.

Enjoy!


More information about the Digitalmars-d mailing list