4.5 Adding a new Attribute

Now we look at an example of how to add a new attribute. As example we consider the set of all primes that divide the size of a group.

First we have to declare the attribute:

PrimesDividingSize:=NewAttribute("PrimesDividingSize",IsGroup);
This implicitly declares attribute tester and setter, it is convenient however to assign these to variables as well:
HasPrimesDividingSize:=Tester(PrimesDividingSize);
SetPrimesDividingSize:=Setter(PrimesDividingSize);
Alternatively, there is a declaration command that executes all three assignments simultaneously and protects the variables against overwriting:
DeclareAttribute("PrimesDividingSize",IsGroup);

Next we have to install method(s) for the attribute that compute its value. (This is not strictly necessary. We could use the attribute also without methods only for storing and retreiving information, but calling it for objects for which the value is not known would produce a ``No method found'' error.) For this purpose we can imagine the attribute simply as an one-argument operation:

InstallMethod(PrimesDividingSize,"for finite groups",
  [IsGroup and IsFinite],
function(G)
  if Size(G)=1 then return [];
  else return Set(Factors(Size(G)));fi;
end);
The function installed must always return a value (or call TryNextMethod. If the object is in the represenattion IsAttributeStoringRep this return value once computed will be automatically stored and retrieved if the attribute is called a second time. We don't have to call setter or tester ourselves. (This storage happens by GAP internally calling the attribute setter with the return value of the function. Retreival is by a high-ranking method which is installed under the condition HasPrimesDividingSize. This method was installed automatically when the attribute was declared.)

[Top] [Previous] [Up] [Next] [Index]

GAP 4 manual
February 2000