# New Ticket Created by  Patrick R. Michaud 
# Please include the string:  [perl #48112]
# in the subject line of all future correspondence about this issue. 
# <URL: http://rt.perl.org/rt3/Ticket/Display.html?id=48112 >


Cloning a Hash also causes its values to be cloned.
Perhaps this is the desired behavior, but if so, it's
inconsistent with cloning other aggregates.

The following program shows that cloning a Hash also clones
the elements in the Hash:

    $ cat z1.pir
    .sub main
        .local pmc value
        value = new 'String'
        value = 'xyz'
    
        # create a hash with the above value
        .local pmc agg0, agg1
        agg0 = new 'Hash'
        agg0['xyz'] = value
    
        # clone it
        agg1 = clone agg0
    
        # now change the value
        value = 'abc'
    
        # see what the hashes say
        $S0 = agg0['xyz']
        say $S0
        $S0 = agg1['xyz']
        say $S0
    .end
    $ ./parrot z1.pir
    abc
    xyz
    $
   
This is consistent with the behavior of at least
ResizablePMCArray, which doesn't clone its elements:

    $ cat z2.pir
    .sub main
        .local pmc value
        value = new 'String'
        value = 'xyz'
    
        # create an array with the above value
        .local pmc agg0, agg1
        agg0 = new 'ResizablePMCArray'
        agg0[12] = value
    
        # clone it
        agg1 = clone agg0
    
        # now change the value
        value = 'abc'
    
        # see what the arrays say
        $S0 = agg0[12]
        say $S0
        $S0 = agg1[12]
        say $S0
    .end
    $ ./parrot z2.pir
    abc
    abc
    $

I think clone ought to be fairly consistent here, whatever it does.
If we decide that clone should have deepcopy semantics, then we
need a note somewhere that explains the recommended way to do
shallow copies.

Also, if we decide to keep the deepcopy clone semantics for
Hash, we need to fix it so that we can clone NULL entries in
the hash (this is how I ran across the problem in the first place).

Thanks,

Pm

Reply via email to