On Monday, 11 October 2021 at 11:43:14 UTC, Stefan Koch wrote:
> Good day to you.
I have decided to release a preview binary for core.reflect.
And I am including the low-level-runtime.
Cheers,
And let me know how you like it.
Wow, this is great! Just ran the demo on WSL2.
There are many uses for it. Off the top of my head, recently Rikki wrote for me a mixin which generated a C ABI for any D class. This could be used in a similar way to generate wrappers very easily I imagine.
It could probably be useful for IDE tooling as well.
mixin template CWrapper(Type) {
import std.traits;
export extern(C):
pragma(mangle, "create_" ~ __traits(identifier, Type))
void* creator(Parameters!(Type.__ctor) args) {
return cast(void*)new Type(args);
}
mixin(() {
string ret;
foreach(m; __traits(allMembers, Type)) {
static if (m != "__ctor" && m != "__dtor" && isFunction!(__traits(getMember, Type, m))) {
ret ~= `ReturnType!(__traits(getMember, Type, "` ~ m ~ `")) ` ~ __traits(identifier, Type) ~ `_` ~ m ~ `(void* obj, Parameters!(__traits(getMember, Type, "` ~ m ~ `")) args) { return (cast(Type)obj).` ~ m ~ `(args); }`;
}
}
return ret;
}());
}
class Foo {
int x;
this(int x) { this.x = x; }
int getX() { return x; }
}
mixin CWrapper!(Foo);
// GENERATED CODE:
extern(C) {
void* create_Foo(int); // returns void* to Foo instance
int Foo_getX(void*); // takes void* to Foo instance
}