www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Traits problem

reply Daniel Ribeiro Maciel <daniel.maciel gmail.com> writes:
Hello!

I'm trying to use __traits in the following manner:

import std.stdio;
import std.traits;

class Foo
{
    void bla() {  }
    void foo() {  }
}

int main()
{
    immutable string members[] = [ "bla", "foo" ];
    writeln( typeid(typeof(__traits(getVirtualFunctions, Foo, members[0]) )) );
}

But this won't compile. The error:

main.d(14): Error: string expected as second argument of __traits
getVirtualFunctions instead of members[0u]

What can I do? I'm trying to hack the dmd source code in order to fix this. 
Can anyone give me any tips on what to do?

Best regards,
Daniel
Dec 04 2009
parent reply bearophile <bearophileHUGS lycos.com> writes:
Daniel Ribeiro Maciel:

Try this:

import std.stdio: writeln;

class Foo {
    float bla() { return 0.0; }
    void foo() {}
}

void main() {
    enum string[] members = ["bla", "foo"];
    writeln( typeid(typeof(__traits(getVirtualFunctions, Foo, members[0]) )) );
}

Output: (float())
You need enum there, unfortunately. Also note the position of [], the lack of
traits import, the qualified import from std.stdio, the void return of main,
and so on.

Bye,
bearophile
Dec 04 2009
parent reply Daniel Ribeiro Maciel <daniel.maciel gmail.com> writes:
Heya!

Ok, let me explain what I want to do:


 import std.stdio: writeln;
 
 class Foo {
     float bla() { return 0.0; }
     void foo() {}
 }
 
 void main() {
     enum string[] members = __traits(allMembers, Foo );
     foreach( member; members )
         writeln( typeid(typeof(__traits(getVirtualFunctions, Foo, member) )) );
}

Is that possible?

Best regards,
Daniel


bearophile Wrote:

 Daniel Ribeiro Maciel:
 
 Try this:
 
 import std.stdio: writeln;
 
 class Foo {
     float bla() { return 0.0; }
     void foo() {}
 }
 
 void main() {
     enum string[] members = ["bla", "foo"];
     writeln( typeid(typeof(__traits(getVirtualFunctions, Foo, members[0]) )) );
 }
 
 Output: (float())
 You need enum there, unfortunately. Also note the position of [], the lack of
traits import, the qualified import from std.stdio, the void return of main,
and so on.
 
 Bye,
 bearophile

Dec 04 2009
next sibling parent reply Daniel Ribeiro Maciel <daniel.maciel gmail.com> writes:
The strange part is that, for some reason, this works:


import std.stdio: writeln;

class Foo {
    float bla() { return 0.0; }
    void foo() {}
}

void exportObject( TClass, members... )()
{
    foreach( uint i, member; members )
        writeln( typeid(typeof(__traits(getVirtualFunctions, Foo, members[i])
)) ); 
}

void main() {
    exportObject!( Foo, "bla", "foo" )();
}

Daniel Ribeiro Maciel Wrote:

 Heya!
 
 Ok, let me explain what I want to do:
 
 
  import std.stdio: writeln;
  
  class Foo {
      float bla() { return 0.0; }
      void foo() {}
  }
  
  void main() {
      enum string[] members = __traits(allMembers, Foo );
      foreach( member; members )
          writeln( typeid(typeof(__traits(getVirtualFunctions, Foo, member) ))
);
 }
 
 Is that possible?
 
 Best regards,
 Daniel
 
 
 bearophile Wrote:
 
 Daniel Ribeiro Maciel:
 
 Try this:
 
 import std.stdio: writeln;
 
 class Foo {
     float bla() { return 0.0; }
     void foo() {}
 }
 
 void main() {
     enum string[] members = ["bla", "foo"];
     writeln( typeid(typeof(__traits(getVirtualFunctions, Foo, members[0]) )) );
 }
 
 Output: (float())
 You need enum there, unfortunately. Also note the position of [], the lack of
traits import, the qualified import from std.stdio, the void return of main,
and so on.
 
 Bye,
 bearophile


Dec 05 2009
parent bearophile <bearophileHUGS lycos.com> writes:
Daniel Ribeiro Maciel:

Note that this foreach is a static foreach because members is a tuple, it's not
an array:

 void exportObject( TClass, members... )()
 {
     foreach( uint i, member; members )
         writeln( typeid(typeof(__traits(getVirtualFunctions, Foo, members[i])
)) ); 
 }

I hope the the future real static foreach will work on enum (constant) arrays too. Bye, bearophile
Dec 05 2009
prev sibling parent Trass3r <mrmocool gmx.de> writes:
Daniel Ribeiro Maciel schrieb:
  void main() {
      enum string[] members = __traits(allMembers, Foo );
      foreach( member; members )
          writeln( typeid(typeof(__traits(getVirtualFunctions, Foo, member) ))
);
 }
 
 Is that possible?
 

__traits is a really immature thing. There's this bug that's been existing since ages: http://d.puremagic.com/issues/show_bug.cgi?id=1386 I posted a strange workaround there. Don't know if it works for getVirtualFunctions as well though.
Jan 02 2010