www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - AliasSeq of T.tupleof for class and all base classes

reply bitwise <bitwise.pvt gmail.com> writes:
As far as I can tell, this code should compile:

class B       { int a; }
class D1 : B  { int b; }
class D2 : D1 { int c; }

template TupleOf(Classes...)
{
     static if(Classes.length > 1)
         alias TupleOf = AliasSeq!(Classes[0].tupleof, 
TupleOf!(Classes[1..$]));
     else static if(Classes.length == 1)
         alias TupleOf = AliasSeq!(Classes[0].tupleof);
     else
         alias TupleOf = AliasSeq!();
}

int main(string[] argv)
{
     alias allClasses = AliasSeq!(D2, BaseClassesTuple!D2);
     alias allFields = TupleOf!allClasses;
     return 0;
}


But I get this:

Error: template instance AliasSeq!(b, a) AliasSeq!(b, a) is 
nested in both D1 and B
Error: template instance main.TupleOf!(D1, B, Object) error 
instantiating
instantiated from here: TupleOf!(D2, D1, B, Object)

Any ideas?

   Thanks
Sep 29
parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
On 9/29/17 11:39 PM, bitwise wrote:
 As far as I can tell, this code should compile:
 
 class B       { int a; }
 class D1 : B  { int b; }
 class D2 : D1 { int c; }
 
 template TupleOf(Classes...)
 {
      static if(Classes.length > 1)
          alias TupleOf = AliasSeq!(Classes[0].tupleof, 
 TupleOf!(Classes[1..$]));
      else static if(Classes.length == 1)
          alias TupleOf = AliasSeq!(Classes[0].tupleof);
      else
          alias TupleOf = AliasSeq!();
 }
 
 int main(string[] argv)
 {
      alias allClasses = AliasSeq!(D2, BaseClassesTuple!D2);
      alias allFields = TupleOf!allClasses;
      return 0;
 }
 
 
 But I get this:
 
 Error: template instance AliasSeq!(b, a) AliasSeq!(b, a) is nested in 
 both D1 and B
 Error: template instance main.TupleOf!(D1, B, Object) error instantiating
 instantiated from here: TupleOf!(D2, D1, B, Object)
 
 Any ideas?
 
    Thanks
I think the problem may be that derived classes' tupleof has some of the same variables as the base class? I agree it should work, but I think if it did work, it may not be what you want. You would see a lot of repeats. BTW, AliasSeq!(x.tupleof) is redundant, x.tupleof is already an AliasSeq. -Steve
Sep 30
next sibling parent bitwise <bitwise.pvt gmail.com> writes:
On Saturday, 30 September 2017 at 12:42:17 UTC, Steven 
Schveighoffer wrote:
 I think the problem may be that derived classes' tupleof has 
 some of the same variables as the base class?

 I agree it should work, but I think if it did work, it may not 
 be what you want. You would see a lot of repeats.
.tupleof doesn't return fields from base classes. assert(D2.tupleof.stringof == "tuple(c)");
Sep 30
prev sibling parent bitwise <bitwise.pvt gmail.com> writes:
On Saturday, 30 September 2017 at 12:42:17 UTC, Steven 
Schveighoffer wrote:
 [...]
https://issues.dlang.org/show_bug.cgi?id=17870
Oct 01