Taking a copy of an object

Tom S h3r3tic at remove.mat.uni.torun.pl
Thu Aug 3 15:53:18 PDT 2006


Hasan Aljudy wrote:
> 
> 
> Tom S wrote:
>> Hasan Aljudy wrote:
>>
>>>
>>>
>>> Tom S wrote:
>>>
>>>> Hasan Aljudy wrote:
>>>>
>>>>>
>>>>> a compiler generated .dup method would be nice.
>>>>
>>>>
>>>>
>>>>
>>>> Reflection and the ability to generate your own would be even better.
>>>
>>>
>>> well you already can create your own .dup
>>> I mean it's just a method that returns typeof(this)
>>
>>
>> There was a subtle difference in what I wrote :) 'generate' vs 
>> 'create'  - that is. Generating own .dup and/or serialization 
>> functions based on meta data is different than writing/creating your 
>> own routines to handle that by hand.
>>
>>
>> -- 
>> Tomasz Stachowiak
> 
> Sorry, what's the difference of writing code and "generating code by 
> yourself"?

I didn't write 'generating code by yourself'. Anyway, in this context:

If you wanted the .dup to behave in such a way that e.g. it would do a 
binary copy of all structs and built-in types, and recursively call .dup 
on all object members of a given class that support the .dup method 
then... you'd have to write it by hand for each class. Also, you'd have 
to update the .dup method upon each modification of the class' members.

On the other hand, if D had good reflection support, it would be 
possible to e.g. define a mixin that detects what fields a given class 
contains and generates your custom-tailored .dup method.


--
Tomasz Stachowiak



More information about the Digitalmars-d mailing list