> < ^ Date: Wed, 27 Jul 1994 11:12:00 +0100 (MET)
> < ^ From: Martin Schoenert <martin.schoenert@math.rwth-aachen.de >
> < ^ Subject: Re: operations query
Andrew Mathas writes in his e-mail message of 1995/07/25

Here is a simple question which must have a simple answer.

What makes you think that a simple question must have a simple answer?
After all there are lots of simple questions in mathematics that don't
seem to have simple answers. And we try to model mathematics as close
as possible with GAP ;-)

He continues

I have defined a record which has the form:
rec (elt := a list, operations := Ops, ...)
where Ops := rec (...). Inside the operations are various functions
defining printing for the record, addition, and so on. I wanted to add a
Length function for these elements so I wrote a Ops.Length function which
of course doesn not work. Evidiently functions like "Print", "+" and so
on are handled differently than other functions. So how do I add a Length
function to this record?

That is true. Only functions and operations that are generally applied
to domains use the dispatching mechanism through the operations record.
Functions that are usually applied to one (or more) of the basic types
(as provided by the kernel), e.g., integers, lists, permutations, etc.,
are not dispatched.

Luckily it is not difficult to work around this. Simply write

OLDLength := Length;
Length := function ( obj )
    if IsList( obj )  then
        return OLDLength( obj );
    else
        return obj.operations.Length( obj );
    fi;
end;

The problem of course is that this makes 'Length' slower for ``standard''
case. Since 'Length' is used quite a lot in the library, this will slow
down everything. I can't say how much, but if you try it, I'd like to
see your results.

He continues

Incidently, unless I have missed it the description of such things does
not appear to be in the manual. Similarly for the use of "arg" in
function definitions. Are these documented anywhere?

The general description is in the section "Dispatchers". The sections
for the dispatchers contain paragraphs like the following

'Orbit' calls \\
'<G>.operations.Orbit( <G>, <d>, <operation> )' \\
and returns the value. Note that the third argument is not optional for
functions called this way.

But this is not done consistently in the manual, so if you find a section
without such a paragraph, it doesn't mean that the function is not a
dispatcher.

In section "Function Calls" you will find

An exception again occurs if the function has only one formal argument
with the name 'arg'. In this case the values of all the actual arguments
are stored in a list and this list is assigned to the new variable
corresponding to the formal argument 'arg'.

Martin.

-- .- .-. - .. -.  .-.. --- ...- . ...  .- -. -. .. -.- .-
Martin Sch"onert,   Martin.Schoenert@Math.RWTH-Aachen.DE,   +49 241 804551
Lehrstuhl D f"ur Mathematik, Templergraben 64, RWTH, 52056 Aachen, Germany

> < [top]