ModuleCopy - Maple Help

ModuleCopy

specify how an object is copied by Object

 Calling Sequence module() option object; export ModuleCopy, ...; ... end module;

Description

 • If an object defines a method named ModuleCopy, that method will be called when a new object of the same class is created via a call to Object.
 • The main reasons to implement a ModuleCopy routine are:
 – to initialize the data members of a newly created object with values different from the data members of the prototype object (potentially based on arguments passed to the Object routine)
 – to perform a deep copy of data members (for example, creating a copy of an rtable member instead of both objects referencing the same rtable)
 • The ModuleCopy function is only useful for modules that are objects (that is, declared with option object).
 • The calling sequence of a ModuleCopy routine is as follows:
 > ModuleCopy::static := proc( new::Object, proto::Object, ... )
 new is the newly created object that is being initialized.  proto is the prototype object that was passed into the call to Object.  Any remaining arguments are the extra arguments passed into the Object routine.
 • Implementing a ModuleApply routine that calls Object to invoke ModuleCopy makes applying the prototype object into an object factory.

Examples

 > module Obj1()    option object;    local data := 0;    export getData::static := proc( self::Obj1 )        self:-data;    end;    export ModuleCopy::static := proc( self::Obj1, proto::Obj1, d, $) if ( _npassed = 2 ) then self:-data := proto:-data; else self:-data := d; end; end; end:  > $\mathrm{getData}\left(\mathrm{Obj1}\right)$  ${0}$ (1)  > $\mathrm{newObj1}≔\mathrm{Object}\left(\mathrm{Obj1}\right)$  ${\mathrm{newObj1}}{≔}{\mathrm{Object<>}}$ (2)  > $\mathrm{getData}\left(\mathrm{newObj1}\right)$  ${0}$ (3)  > $\mathrm{newObj2}≔\mathrm{Object}\left(\mathrm{Obj1},10\right)$  ${\mathrm{newObj2}}{≔}{\mathrm{Object<>}}$ (4)  > $\mathrm{getData}\left(\mathrm{newObj2}\right)$  ${10}$ (5)  > module Obj2() option object; local data := 0; export getData::static := proc( self::Obj2 ) self:-data; end; export ModuleApply::static := proc( ) Object( Obj2, _passed ); end; export ModuleCopy::static := proc( self::Obj2, proto::Obj2, d,$ )        if ( _npassed = 2 ) then            self:-data := proto:-data;        else            self:-data := d;        end;    end; end:
 > $\mathrm{newObj1}≔\mathrm{Obj2}\left(\right)$
 ${\mathrm{newObj1}}{≔}{\mathrm{Object<>}}$ (6)
 > $\mathrm{getData}\left(\mathrm{newObj1}\right)$
 ${0}$ (7)
 > $\mathrm{newObj2}≔\mathrm{Obj2}\left(10\right)$
 ${\mathrm{newObj2}}{≔}{\mathrm{Object<>}}$ (8)
 > $\mathrm{getData}\left(\mathrm{newObj2}\right)$
 ${10}$ (9)

Compatibility

 • The ModuleCopy command was introduced in Maple 16.