I, too, was enamored with Go Interfaces and implemented them for
.NET:
http://www.codeproject.com/Articles/87991/Dynamic-interfaces-in-any-NET-language
And I wasn't the only one; later, someone else published another
library for .NET with the exact same goal. This is definitely a
feature I would want to see in D, preferably as a first-class
feature, although sadly that would break any code that relies on
ISomething being pointer-sized; Go uses fat pointers, and we use
a thin-pointer implementation in .NET but it's inefficient (as
every cast creates a heap-allocated wrapper, and
double-indirection is needed to reach the real method.)
Anyway, they say it's possible to build runtime reflection in D
but I've no idea how... has it never been done before?
Of course, runtime template instantiation won't be possible.
Therefore, run-time casting will have to be more limited than
compile-time casting.
Reflection to free functions would be really nice, but it might
be less capable at run-time. Consider if you there is a class A
in third-party module MA that you want to cast to interface I,
but class A is missing a function F() from I. So in your module
(module MB) you define a free function F(B) and now you can do
the cast. I guess realistically this can only happen at
compile-time, since a run-time cast would naturally only look in
module MA, not MB, for functions it could use to perform the
cast. Presumably, it also requires that MA requested a run-time
reflection table to be built, and is it possible to build a
reflection table for a module over which you have no control?

Interesting, good article and project, thanks.
And I wasn't the only one; later, someone else published another library

for .NET with the exact same goal. This is definitely a feature I would
want to see in D, preferably as a first-class feature, although sadly that
would break any code that relies on ISomething being pointer-sized; Go uses
fat pointers, and we use a thin-pointer implementation in .NET but it's
inefficient (as every cast creates a heap-allocated wrapper, and
double-indirection is needed to reach the real method.)

Anyway, they say it's possible to build runtime reflection in D but I've
no idea how... has it never been done before?

Of course, runtime template instantiation won't be possible. Therefore,
run-time casting will have to be more limited than compile-time casting.
Reflection to free functions would be really nice, but it might be less
capable at run-time. Consider if you there is a class A in third-party
module MA that you want to cast to interface I, but class A is missing a
function F() from I. So in your module (module MB) you define a free
function F(B) and now you can do the cast. I guess realistically this can
only happen at compile-time, since a run-time cast would naturally only
look in module MA, not MB, for functions it could use to perform the cast.
Presumably, it also requires that MA requested a run-time reflection table
to be built, and is it possible to build a reflection table for a module
over which you have no control?

Free functions support is hard, with runtime cast, I just think compile
time.
I am trying to support free functions at compile time, is also hard, since
the generator in gointerface module, but other modules are only visible in
the module that used these modules. I have an ugly implementation used
compile time string mixin, trying to simplify it. Fighting.

Free functions support is hard, with runtime cast, I just think compile
time.

It's possibly to implement runtime reflection by loading the running
executable and inspecting the symbol table. It's an ugly hack but it
should work.
http://flectioned.kuehne.cn/

I am trying to support free functions at compile time, is also hard,
since the generator in gointerface module, but other modules are only
visible in the module that used these modules. I have an ugly
implementation used compile time string mixin, trying to simplify it.
Fighting.

I think you can pass a module to a template via an alias parameter. Then
the template should be able to inspect all free functions using
something like __traits(allMembers).
--
/Jacob Carlborg

On 2012-07-18 06:10, lijie wrote:
Free functions support is hard, with runtime cast, I just think compile

time.

It's possibly to implement runtime reflection by loading the running
executable and inspecting the symbol table. It's an ugly hack but it should
work.
http://flectioned.kuehne.cn/

It is an optional way. I want to do all thing in D code.

I am trying to support free functions at compile time, is also hard,

since the generator in gointerface module, but other modules are only
visible in the module that used these modules. I have an ugly
implementation used compile time string mixin, trying to simplify it.
Fighting.

I think you can pass a module to a template via an alias parameter. Then
the template should be able to inspect all free functions using something
like __traits(allMembers).

I think you can pass a module to a template via an alias parameter. Then
the template should be able to inspect all free functions using something
like __traits(allMembers).

Have a problem.
--
// file: A.d
module A;
// functions and classes
--
--
// file: testtraits.d
import std.stdio;
import A;
void main() {
writeln(__traits(allMembers, A));
}
--
There is a compilation error:
testtraits.d(6): Error: import A has no members
If module is under a package name, it is OK, is that a bug?

On Wed, Jul 18, 2012 at 2:59 PM, Jacob Carlborg <doob me.com
<mailto:doob me.com>> wrote:
I think you can pass a module to a template via an alias parameter.
Then the template should be able to inspect all free functions using
something like __traits(allMembers).
Have a problem.
--
// file: A.d
module A;
// functions and classes
--
--
// file: testtraits.d
import std.stdio;
import A;
void main() {
writeln(__traits(allMembers, A));
}
--
There is a compilation error:
testtraits.d(6): Error: import A has no members
If module is under a package name, it is OK, is that a bug?