www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - struct/class generation

reply ANtlord <antlord92 gmail.com> writes:
Hello dear community!

I've met a little issue. How can I generate struct or class 
copying some fields and methods from another struct or class? 
I've found methods to get fields. They are 
std.traits.FieldNameTuple and std.traits.FieldTypeTuple but I 
can't find a method allows getting methods from struct or class.

Other words I want to get fields and methods by string values and 
copy them to my struct. Is it possible? Or do I want something 
strange?

Thanks in advance. Sorry if my English is not clear.
Oct 11
next sibling parent rikki cattermole <rikki cattermole.co.nz> writes:
On 11/10/2017 12:37 PM, ANtlord wrote:
 Hello dear community!
 
 I've met a little issue. How can I generate struct or class copying some 
 fields and methods from another struct or class? I've found methods to 
 get fields. They are std.traits.FieldNameTuple and 
 std.traits.FieldTypeTuple but I can't find a method allows getting 
 methods from struct or class.
 
 Other words I want to get fields and methods by string values and copy 
 them to my struct. Is it possible? Or do I want something strange?
 
 Thanks in advance. Sorry if my English is not clear.
There is no way to get string versions of function bodies.
Oct 11
prev sibling parent reply drug <drug2004 bk.ru> writes:
11.10.2017 14:37, ANtlord пишет:
 Hello dear community!
 
 I've met a little issue. How can I generate struct or class copying some 
 fields and methods from another struct or class? I've found methods to 
 get fields. They are std.traits.FieldNameTuple and 
 std.traits.FieldTypeTuple but I can't find a method allows getting 
 methods from struct or class.
 
 Other words I want to get fields and methods by string values and copy 
 them to my struct. Is it possible? Or do I want something strange?
 
 Thanks in advance. Sorry if my English is not clear.
You need a wrapper, but it would include all fields and methods from base struct/class and you just expose only that you need. So it's quite possible except I don't known how you can exclude needless fields. But FieldNameTuple etc are not enough, your best friend is __traits(allMember, ...), iterating over its output and making decision what current symbol is and processing it according to its features. It's not trivial but not very complex.
Oct 11
parent Daniel Kozak <kozzi11 gmail.com> writes:
If you just want to not repeat fields and methods you can use alias this or
mixins:
https://run.dlang.io/is/0UkjTe

On Wed, Oct 11, 2017 at 2:07 PM, drug via Digitalmars-d-learn <
digitalmars-d-learn puremagic.com> wrote:

 11.10.2017 14:37, ANtlord =D0=BF=D0=B8=D1=88=D0=B5=D1=82:

 Hello dear community!
 I've met a little issue. How can I generate struct or class copying some
 fields and methods from another struct or class? I've found methods to g=
et
 fields. They are std.traits.FieldNameTuple and std.traits.FieldTypeTuple
 but I can't find a method allows getting methods from struct or class.

 Other words I want to get fields and methods by string values and copy
 them to my struct. Is it possible? Or do I want something strange?

 Thanks in advance. Sorry if my English is not clear.
You need a wrapper, but it would include all fields and methods from base struct/class and you just expose only that you need. So it's quite possib=
le
 except I don't known how you can exclude needless fields.
 But FieldNameTuple etc are not enough, your best friend is
 __traits(allMember, ...), iterating over its output and making decision
 what current symbol is and processing it according to its features. It's
 not trivial but not very complex.
Oct 11