On 01/21/2011 02:02 PM, spir wrote:
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


What you're saying doesn't make sense. :-)

You welcome them by saying they can copy a class object by using copyMembers. Newcomers may not write that code, but using it is dead simple.

I suspect most D users couldn't implement floating point formatting either, or a bunch of other hard stuff.

Reply via email to