www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - The possibility of a DLang dynamic transfer class type and a member

reply zoujiaqing <zoujiaqing gmail.com> writes:
Like main function's code:


```D
import std.stdio;

interface Base
{
     int count();
}

class A : Base
{
     int a = 1;
     int b = 4;

     int count()
     {
         return a;
     }
}

class B : Base
{
     int b = 2;
     int a = 3;

     int count()
     {
         return b;
     }
}

void test(Type classType, Type propertyType1, Type peropertyType2)
{
     // ...
}

void main()
{
     Type class1;
     Type class2;

     class1 = A;
     class2 = B;
     class3 = A;

     auto obj1 = new class1;
     auto obj2 = new class2;

     if (true)
     {
         Base obj = cast(Base) obj1;
         writeln(obj.count());

         Base obj = cast(Base) obj2;
         writeln(obj.count());
     }

     assert(checkClass(class1, class2));
     assert(checkClass(class1, class3));

     test(A, A.a, B.b);
}
```
Nov 14
parent reply Paul Backus <snarwin gmail.com> writes:
On Thursday, 14 November 2019 at 08:47:15 UTC, zoujiaqing wrote:
 Like main function's code:

 [...]

 void main()
 {
     Type class1;
     Type class2;

     class1 = A;
     class2 = B;
     class3 = A;

     auto obj1 = new class1;
     auto obj2 = new class2;

     if (true)
     {
         Base obj = cast(Base) obj1;
         writeln(obj.count());

         Base obj = cast(Base) obj2;
         writeln(obj.count());
     }

     assert(checkClass(class1, class2));
     assert(checkClass(class1, class3));

     test(A, A.a, B.b);
 }
 ```
You can do something pretty similar with TypeInfo: void main() { TypeInfo_Class class1; TypeInfo_Class class2; class1 = typeid(A); class2 = typeid(B); auto obj1 = class1.create; auto obj2 = class2.create; if (true) { { Base obj = cast(Base) obj1; writeln(obj.count()); // 1 } { Base obj = cast(Base) obj2; writeln(obj.count()); // 2 } } }
Nov 14
parent reply zoujiaqing <zoujiaqing gmail.com> writes:
On Thursday, 14 November 2019 at 21:03:28 UTC, Paul Backus wrote:
 On Thursday, 14 November 2019 at 08:47:15 UTC, zoujiaqing wrote:
 Like main function's code:

 [...]

 void main()
 {
     Type class1;
     Type class2;

     class1 = A;
     class2 = B;
     class3 = A;

     auto obj1 = new class1;
     auto obj2 = new class2;

     if (true)
     {
         Base obj = cast(Base) obj1;
         writeln(obj.count());

         Base obj = cast(Base) obj2;
         writeln(obj.count());
     }

     assert(checkClass(class1, class2));
     assert(checkClass(class1, class3));

     test(A, A.a, B.b);
 }
 ```
You can do something pretty similar with TypeInfo: void main() { TypeInfo_Class class1; TypeInfo_Class class2; class1 = typeid(A); class2 = typeid(B); auto obj1 = class1.create; auto obj2 = class2.create; if (true) { { Base obj = cast(Base) obj1; writeln(obj.count()); // 1 } { Base obj = cast(Base) obj2; writeln(obj.count()); // 2 } } }
I want get property type, like this: auto testType1 = A.a; auto testType2 = B.a; if (testType1 is testType2) { writeln("yes"); }
Nov 15
parent Basile B. <b2.temp gmx.com> writes:
On Friday, 15 November 2019 at 09:06:35 UTC, zoujiaqing wrote:
 On Thursday, 14 November 2019 at 21:03:28 UTC, Paul Backus 
 wrote:
 On Thursday, 14 November 2019 at 08:47:15 UTC, zoujiaqing 
 wrote:
 [...]
You can do something pretty similar with TypeInfo: void main() { TypeInfo_Class class1; TypeInfo_Class class2; class1 = typeid(A); class2 = typeid(B); auto obj1 = class1.create; auto obj2 = class2.create; if (true) { { Base obj = cast(Base) obj1; writeln(obj.count()); // 1 } { Base obj = cast(Base) obj2; writeln(obj.count()); // 2 } } }
I want get property type, like this: auto testType1 = A.a; auto testType2 = B.a; if (testType1 is testType2) { writeln("yes"); }
In D we rather do these kind of stuff statically, using template parameters and constraints, e.g as constraint if(__traits(hasMember)).
Nov 15