ModuleCopy
specify how an object is copied by Object
Calling Sequence
Description
Examples
Compatibility
module() option object; export ModuleCopy, ...; ... end module;
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.
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:
getData⁡Obj1
0
newObj1≔Object⁡Obj1
newObj1≔Object<<Obj1,139729098329280>>
getData⁡newObj1
newObj2≔Object⁡Obj1,10
newObj2≔Object<<Obj1,139729098297376>>
getData⁡newObj2
10
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:
newObj1≔Obj2⁡
newObj1≔Object<<Obj2,139729097491552>>
newObj2≔Obj2⁡10
newObj2≔Object<<Obj2,139729097471040>>
The ModuleCopy command was introduced in Maple 16.
For more information on Maple 16 changes, see Updates in Maple 16.
See Also
module
ModuleApply
Object
Object,create
Object,overview
procedure
rtable
Download Help Document