Hi all,<br><br>This has probably been asked a few times before, but I couldn't find anything relevant.<br>What is the best way to create a generic system to clone an object? (am I missing something obvious?)<br><br>You can still of course create a `copy' method that must be overridden, but this can be risky if the class gets extended and some initializations may be forgotten, or when creating a new child class and forgetting to override the method.<br>
<br>So I tried to use (inspect #f) and using `struct-copy', which worked, but that is awful.<br><br>Another way is to use a modified `init-field' form which can take a source object of the same class and initializes the fields with the values of the object or with a default value. Just a quick first try:<br>
<br><blockquote style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;" class="gmail_quote"><span style="font-family: courier new,monospace;">(define-syntax-rule (init-field-clone source [var val] ...)<br>
(begin<br> (init [source #f])<br> (init-field [var (if source (get-field var source) val)] ...)))<br></span></blockquote><blockquote style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;" class="gmail_quote">
<div> </div></blockquote><blockquote style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;" class="gmail_quote"><div> ; Tests:<br></div></blockquote><blockquote style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;" class="gmail_quote">
<span style="font-family: courier new,monospace;">> (define c1%</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> (class object%</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> (init-field-clone source [x 1] [y 2])</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> (super-new)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> (define/public (vals)(list x y))</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> ))</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">> (define o1 (new c1% [x "x"] [y "y"]))</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">> (send o1 vals)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">("x" "y")</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">> (define o2 (new c1% [source o1]))</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">> (send o2 vals)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">("x" "y")</span><br></blockquote><br>So that simple form works too but it limits the class model (and I guess it would be a bit cumbersome to take it all into account).<br>
And, BTW, despite the use of `define-syntax-rule', when omitting the `source' argument in the `init-field-clone' definition,<br>the form still works but becomes non-hygienic! Now I have to completely rewire my brain again...<br>
<br>Probably another way could be to serialize/deserialize, but I don't think that is the purpose of serialization...<br><br>My question is thus: do the Scheme gurus know the best generic way to make an object cloning system?<br>
<br>Thanks,<br>Laurent<br>