On 01/21/2011 04:02 AM, Andrei Alexandrescu wrote:
That being said, it's not difficult to define a generic function that
copies fields over from one class object to another. Here's a start:

import std.stdio;

void copyMembers(A)(A src, A tgt) if (is(A == class)) {
     foreach (e; __traits(allMembers, A)) {
         static if (!is(typeof(__traits(getMember, src, e)) == function)
&& e != "Monitor")
         {
             __traits(getMember, tgt, e) = __traits(getMember, src, e);
         }
     }
}

<side-note>
How many programmers in the world consider this kind of code "not difficult"? These few lines mix 3 features I personly find, say, 'special': generics using 'is' constraints, static if, __traits.
<personal> From those 3, static if is not ugly. </personal>

Is code like this supposed to be considered normal? Where is D2 speak going to? Merge into this a handful of range/algos, a pinch of happy qualifiers here & there, a specimen of... string-mixin ;-)

How to welcome newcomers?
</side-note>

Denis
_________________
vita es estrany
spir.wikidot.com

Reply via email to