www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - D and reflection

reply "amehat" <amehat gmail.com> writes:
Hello everyone,

I'm working on porting a java library in D, and I stuck on a 
class because it works with the reflection.

 From what I've read on prowiki 
(http://www.prowiki.org/wiki4d/wiki.cgi?LanguagesVersusD), D can 
not do reflection, it is limited to the Run-Time Type Information 
(RTTI ) (http://dlang.org/traits.html).

Here is the piece of code that blocks me:

static {
         Class clazz = this.class;

         auto accessible2Args = new Callback(clazz, 
"accessibleProc", 2);
         auto proc2Args = accessible2Args.getAddress();
         if (proc2Args === 0) {
		(...)
	}
(...)
}

I am looking for a workaround that I have not found yet.
If someone an idea on the subject, I'm interested.

Thank you to you.
May 07 2014
next sibling parent "John Colvin" <john.loughran.colvin gmail.com> writes:
On Wednesday, 7 May 2014 at 16:51:10 UTC, amehat wrote:
 Hello everyone,

 I'm working on porting a java library in D, and I stuck on a 
 class because it works with the reflection.

 From what I've read on prowiki 
 (http://www.prowiki.org/wiki4d/wiki.cgi?LanguagesVersusD), D 
 can not do reflection, it is limited to the Run-Time Type 
 Information (RTTI ) (http://dlang.org/traits.html).

 Here is the piece of code that blocks me:

 static {
         Class clazz = this.class;

         auto accessible2Args = new Callback(clazz, 
 "accessibleProc", 2);
         auto proc2Args = accessible2Args.getAddress();
         if (proc2Args === 0) {
 		(...)
 	}
 (...)
 }

 I am looking for a workaround that I have not found yet.
 If someone an idea on the subject, I'm interested.

 Thank you to you.
Firstly, the the wiki has moved: wiki.dlang.org D is very powerful at compile-time reflection, but less powerful at runtime. I'm afraid I don't quite understand what you need in your example. http://forum.dlang.org/group/digitalmars.D.learn would be a more appropriate place for your question.
May 07 2014
prev sibling next sibling parent "Adam D. Ruppe" <destructionator gmail.com> writes:
On Wednesday, 7 May 2014 at 16:51:10 UTC, amehat wrote:
 D  an not do reflection
It can, but it is different than Java. D can look at class members at compile time, and can build up runtime tables, but it takes a few tricks to do things like get methods of a derived class.
 it is limited to the Run-Time Type Information (RTTI ) 
 (http://dlang.org/traits.html).
That's actually compile time introspection docs, the run time stuff is found in http://dlang.org/phobos/object.html#TypeInfo (and is useless for looking at methods btw) To find methods that take two arguments, you'd do something like this: alias helper(alias A) = A; // just to shorten things later foreach(memberName; __traits(allMembers, YourClass)) { alias member = helper!(__traits(getMember, YourClass, memberName)); static if(is(member == function)) { import std.traits; ParameterTypeTuple!member args; static if(args.length == 2) { // found one if(memberName == "accessibleProc") { // you can check the name too } } } } tbh, I don't know exactly what the Java does but the D things I used are documented here: http://dlang.org/phobos/std_traits.html#ParameterTypeTuple and http://dlang.org/traits.html#allMembers the sample chapter for my D book isn't available yet :( I covered this stuff in more depth there. But actually calling the member gets a bit tricky since you need to attach this to it. You'd do __traits(getMember, some_instance_object, memberName)(args...). That can also be wrapped in a delegate to transform arguments, box/unbox, and such generically too, which is probably closer to what Java does. I don't think this is in the standard library, you'd have to DIY though. Again, this won't just work with child classes, so it might not even be what you need. To work with child classes, you'd want to build an array of the delegates for runtime use or something, and also generate them for each derived instance in the module that defines them. If this is looking like it might be what you need, I can help with that too.
May 07 2014
prev sibling next sibling parent Paulo Pinto <pjmlp progtools.org> writes:
Am 07.05.2014 18:51, schrieb amehat:
 Hello everyone,

 I'm working on porting a java library in D, and I stuck on a class
 because it works with the reflection.

  From what I've read on prowiki
 (http://www.prowiki.org/wiki4d/wiki.cgi?LanguagesVersusD), D can not do
 reflection, it is limited to the Run-Time Type Information (RTTI )
 (http://dlang.org/traits.html).

 Here is the piece of code that blocks me:

 static {
          Class clazz = this.class;

          auto accessible2Args = new Callback(clazz, "accessibleProc", 2);
          auto proc2Args = accessible2Args.getAddress();
          if (proc2Args === 0) {
          (...)
      }
 (...)
 }

 I am looking for a workaround that I have not found yet.
 If someone an idea on the subject, I'm interested.

 Thank you to you.
D can do reflection, but only at compile time. If you can use the class name directly, a possibility might be something like: import std.traits; import std.stdio; class test { void accessibleProc () {} } void main () { auto t = new test(); alias MemberFunctionsTuple!(test, "accessibleProc") accessible2Args; if (accessible2Args.length == 1) { // accessibleProc address in accessible2Args[0] } } If not, it is better if someone with more experience can step in. -- Paulo
May 07 2014
prev sibling next sibling parent reply "Kapps" <opantm2+spam gmail.com> writes:
On Wednesday, 7 May 2014 at 16:51:10 UTC, amehat wrote:
 Hello everyone,

 I'm working on porting a java library in D, and I stuck on a 
 class because it works with the reflection.

 From what I've read on prowiki 
 (http://www.prowiki.org/wiki4d/wiki.cgi?LanguagesVersusD), D 
 can not do reflection, it is limited to the Run-Time Type 
 Information (RTTI ) (http://dlang.org/traits.html).

 Here is the piece of code that blocks me:

 static {
         Class clazz = this.class;

         auto accessible2Args = new Callback(clazz, 
 "accessibleProc", 2);
         auto proc2Args = accessible2Args.getAddress();
         if (proc2Args === 0) {
 		(...)
 	}
 (...)
 }

 I am looking for a workaround that I have not found yet.
 If someone an idea on the subject, I'm interested.

 Thank you to you.
You can generate reflection data at compile-time to use at runtime. For an example, you can take a look at https://shardsoft.com/stash/projects/SHARD/repos/shardtools/browse/source/ShardTools/Reflection.d (docs: https://shardsoft.com/docs/ShardTools/Reflection.html). I wouldn't use the file/repo directly, but feel free to take anything you find useful from it.
May 07 2014
parent reply "Domain" <dont_email empty.com> writes:
On Wednesday, 7 May 2014 at 18:24:23 UTC, Kapps wrote:
 On Wednesday, 7 May 2014 at 16:51:10 UTC, amehat wrote:
 Hello everyone,

 I'm working on porting a java library in D, and I stuck on a 
 class because it works with the reflection.

 From what I've read on prowiki 
 (http://www.prowiki.org/wiki4d/wiki.cgi?LanguagesVersusD), D 
 can not do reflection, it is limited to the Run-Time Type 
 Information (RTTI ) (http://dlang.org/traits.html).

 Here is the piece of code that blocks me:

 static {
        Class clazz = this.class;

        auto accessible2Args = new Callback(clazz, 
 "accessibleProc", 2);
        auto proc2Args = accessible2Args.getAddress();
        if (proc2Args === 0) {
 		(...)
 	}
 (...)
 }

 I am looking for a workaround that I have not found yet.
 If someone an idea on the subject, I'm interested.

 Thank you to you.
You can generate reflection data at compile-time to use at runtime. For an example, you can take a look at https://shardsoft.com/stash/projects/SHARD/repos/shardtools/browse/source/ShardTools/Reflection.d (docs: https://shardsoft.com/docs/ShardTools/Reflection.html). I wouldn't use the file/repo directly, but feel free to take anything you find useful from it.
I think something like this should be included in phobos
May 07 2014
parent "amehat" <amehat gmail.com> writes:
On Thursday, 8 May 2014 at 01:52:05 UTC, Domain wrote:
 On Wednesday, 7 May 2014 at 18:24:23 UTC, Kapps wrote:
 On Wednesday, 7 May 2014 at 16:51:10 UTC, amehat wrote:
 Hello everyone,

 I'm working on porting a java library in D, and I stuck on a 
 class because it works with the reflection.

 From what I've read on prowiki 
 (http://www.prowiki.org/wiki4d/wiki.cgi?LanguagesVersusD), D 
 can not do reflection, it is limited to the Run-Time Type 
 Information (RTTI ) (http://dlang.org/traits.html).

 Here is the piece of code that blocks me:

 static {
       Class clazz = this.class;

       auto accessible2Args = new Callback(clazz, 
 "accessibleProc", 2);
       auto proc2Args = accessible2Args.getAddress();
       if (proc2Args === 0) {
 		(...)
 	}
 (...)
 }

 I am looking for a workaround that I have not found yet.
 If someone an idea on the subject, I'm interested.

 Thank you to you.
You can generate reflection data at compile-time to use at runtime. For an example, you can take a look at https://shardsoft.com/stash/projects/SHARD/repos/shardtools/browse/source/ShardTools/Reflection.d (docs: https://shardsoft.com/docs/ShardTools/Reflection.html). I wouldn't use the file/repo directly, but feel free to take anything you find useful from it.
I think something like this should be included in phobos
Thank you all for your responses and responsiveness. Indeed, John Colvin, I did not know what forum posted this. The next time I would do in D.learn, sorry for this mistake. Many of your proposal works perfectly. I can continue porting my library.
May 08 2014
prev sibling parent Jacob Carlborg <doob me.com> writes:
On 2014-05-07 18:51, amehat wrote:
 Hello everyone,

 I'm working on porting a java library in D, and I stuck on a class
 because it works with the reflection.

  From what I've read on prowiki
 (http://www.prowiki.org/wiki4d/wiki.cgi?LanguagesVersusD), D can not do
 reflection, it is limited to the Run-Time Type Information (RTTI )
 (http://dlang.org/traits.html).

 Here is the piece of code that blocks me:

 static {
          Class clazz = this.class;

          auto accessible2Args = new Callback(clazz, "accessibleProc", 2);
          auto proc2Args = accessible2Args.getAddress();
          if (proc2Args === 0) {
          (...)
      }
 (...)
 }

 I am looking for a workaround that I have not found yet.
 If someone an idea on the subject, I'm interested.

 Thank you to you.
It depends on how the rest of the code looks like. If you're lucky you can just take the address of "accessibleProc". You need to either provide more code or you can have a look at DWT [1], which is port of the Java library SWT. It actually contains code exactly like that. Have a look at this Java file [2] and the translation do D [3]. I'm using a static method here, it's not certain the same will work for you. [1] https://github.com/d-widget-toolkit/dwt-mac [2] http://git.eclipse.org/c/platform/eclipse.platform.swt.git/tree/bundles/org.eclipse.swt/Eclipse%20SWT%20Accessibility/cocoa/org/eclipse/swt/accessibility/SWTAccessibleDelegate.java?id=v3550 [3] https://github.com/d-widget-toolkit/dwt-mac/blob/master/dwt/accessibility/SWTAccessibleDelegate.d -- /Jacob Carlborg
May 07 2014