On Wed, Sep 9, 2009 at 3:38 PM, Hugh Aguilar<[email protected]> wrote:
> Your clone doc says this:
> "Outputs a new object equal to the given object. This is not guaranteed to
> actually copy the object; it does nothing with immutable objects, and does
> not copy words either. However, sequences and tuples can be cloned to obtain
> a shallow copy of the original."
>
> I am confused as to what these limitation mean.
First of all, make sure you understand object identity, and the eq?
word, and what it means for two objects to share structure.
> What does "does not copy
> words" mean?
It means that \ + clone does nothing, and just returns the original
word '+' (rather than making a new word with the same name).
> If any of the slots contain objects rather than primitive
> datums, will they get their data moved over as well?
By default, slots are not cloned. Here is an example,
TUPLE: person name children ;
! Make a person
person new "Jane" >>name { "Joe" } >>children
! Make a copy
dup clone
! Set the first child of the copy
children>> "Britney" swap set-first
! Get the first child of the original
children>> first .
! Not what we expected?
=> "Britney"
If you want 'clone' on a tuple to clone slot values too, define a new method,
M: person clone
call-next-method
[ clone ] change-children ;
> I don't want to just clone a tuple, but change its type as well. If I have a
> child tuple that is derived from the parent tuple but has some extra slots,
> I want to clone an object of the parent type but simultaneously make it an
> object of the child type so that I can then manually fill in the extra
> slots, but have all of the original slots copied over automatically during
> the cloning process. Another possibility would be to just clone the tuple of
> the parent type using your clone word, but then have a word that converts it
> into the child type leaving the data alone in the original slots but
> appending the extra slots onto the object empty of data and ready to be
> manually filled in. Ideas?
You cannot use clone to change an object's type. You should instead
write code which extracts the relevant slots from the parent and
creates a new instance of the child. Alternatively, redesign your code
so that instead of the child inheriting from the parent, it has a slot
which holds the parent. Then your operation becomes even easier, just
make a new instance of a child that holds a reference to the parent.
> I don't want to have to manually fill in all of the original slots because
> the parent type is defined in another file and I don't necessarily know what
> all slots it contains, or what they are for.
You can use macros or other meta-programming techniques to avoid
writing boilerplate in this case. Also take a look at the tuple>array
and >tuple words.
> I don't want a deep copy because my lists are circular and trying to copy
> the links will result in infinite recursion. I already wrote a word to clone
> a linked list by creating a new linked list with new link data.
You can define a new method on the clone generic word for your list
class, instead of making it a new word altogether.
> The problem
> is that I want the nodes in the new linked list to be of some child type
> rather than the parent type of the nodes in the source linked-list.
Sounds like you want to write a 'map' combinator that operates on your
list type.
Slava
------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now. http://p.sf.net/sfu/bobj-july
_______________________________________________
Factor-talk mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/factor-talk