> < ^ Date: Sun, 05 Dec 1999 21:14:54 +0100 (CET)
> < ^ From: Sergei.Haller@math.uni-giessen.de < >
^ Subject: Mutable and immutable objects

Dear forum,

I have some questions obout mutable and immutable objects

here is the declarations/implementations of MyObj

DeclareCategory("IsMyObj", IsObject);
DeclareRepresentation( "IsMyRep", IsAttributeStoringRep, [ "x" ] );
DeclareGlobalFunction( "MyObj" );

InstallGlobalFunction( MyObj,
function(y)
local Fam;

Fam:= NewFamily( "MyFamily", IsMyObj );

     return Objectify( NewType( Fam, IsMyObj and IsMyRep ),
                       Immutable(rec ( x := y ))
               );
end);
     
gap> elem := MyObj([11..15]);
<object>
gap> IsMutable(elem);
false
gap> IsMutable(elem!.x);
false
gap> elem!.x[1] := 21;
# -> Error: Lists Assignment: <list> must be a mutable list
# that error is ok, because the list is not mutable
gap> elem!.x := 21;
21                     # but why this? the underlying rec was made immutable
gap> elem!.y := "hallo";
"hallo"                # or why is this possible?
gap>

Here is my first question:
how to make sure, my object is really immutable?

gap> elem1:=StructuralCopy(elem);
<object>
gap> IsIdenticalObj(elem, elem1);
true                          # of course, IsMutable returns false, so
                              # StructuralCopy returns the object itself
gap> elem1!.x :=22;
22
gap> elem!.x; elem1!.x;
22
22
gap>

Here is my second question:
how to make an object mutable, and bring StructuralCopy to
return a *new* copy of the object
here is an answer (is this the right one?):
Install other function for "MyObj".
This one returns mutable objects and StructuralCopy returns realy
new objects

InstallGlobalFunction( MyObj,
function(y)
local Fam;

Fam:= NewFamily( "MyFamily", IsMyObj );

     return Objectify( NewType( Fam, IsMyObj and IsMutable and IsMyRep ),
                       rec ( x := y )
               );
end);

gap> elem := MyObj([11..15]);
<object>
gap> IsMutable(elem);
true
gap> IsMutable(elem!.x);
true
gap> MakeImmutable(elem);
gap> IsMutable(elem);
false
gap> IsMutable(elem!.x);
true
gap>

but here is my third question:
how to make MakeImmutable return a really immutable object
I would write my own function MyMakeImmutable (AFAIK it is
not possible to override global functions like operations
depending on the type of the arguments).
But this wouldn't be a fine programming style and
this returns us to the first question:
how to make sure, my object is really immutable?

Thanks in advance

Sergei

--------------------------------------------------------------------
         eMail:       Sergei.Haller@math.uni-giessen.de
      WWW-page:     http://www.hrz.uni-giessen.de/~gc1007/
--------------------------------------------------------------------
Be careful of reading health books, you might die of a misprint.
                -- Mark Twain

> < [top]