* contrib: goops doc: calling next-method
@ 2006-03-07 22:36 Marco Maggi
2006-03-11 12:16 ` Neil Jerram
0 siblings, 1 reply; 3+ messages in thread
From: Marco Maggi @ 2006-03-07 22:36 UTC (permalink / raw)
Ciao,
I propose the following to be appended to the
Next-method node in the GOOPS tutorial Texinfo.
@noindent
In the following example a class and a superclass define the
same
method:
@example
(use-modules (oop goops))
(define-class <a> ()
one)
(define-class <b> (<a>)
two)
(define-method (alpha (a <a>))
"alpha a")
(define-method (alpha (b <b>))
(list (next-method) "alpha b"))
@end example
@noindent
in the body of the @code{alpha<b>} function it is fine to invoke
@code{next-method} expecting it to invoke @code{alpha<a>}
because both
the @code{alpha} functions take the same number of
arguments. So the
result will be:
@example
(alpha (make <b>))
;; -> ("alpha a" "alpha b")
@end example
With the following definitions: the body of @code{alpha<b>}
invokes the
@code{alpha} function and an infinite loop will result:
@example
(define-method (alpha (b <b>))
(list (alpha b) "alpha b"))
@end example
@noindent
the most specialised @code{alpha} function is
@code{alpha<b>} itself.
With the following definitions:
@example
(use-modules (oop goops))
(define-class <a> ()
one)
(define-class <b> (<a>)
two)
(define-method (alpha (a <a>) c)
(list "alpha a" c))
(define-method (alpha (b <b>))
(list (next-method) "alpha b"))
@end example
@noindent
the code:
@example
(alpha (make <b>))
@end example
@noindent
will raise an error: the invocation of @code{alpha<b>} has
selected the
@code{alpha} functions that take only one parameter; the
invocation of
@code{next-method} in the body of @code{alpha<b>} will not
find any
other less specialised method; we will get an error even
with the
following:
@example
(define-method (alpha (b <b>))
(list (next-method "string") "alpha b"))
@end example
It is possible to invoke @code{alpha<a>} by evaluating the
@code{alpha}
variable directly, with the appropriate number of parameters:
@example
(define-method (alpha (b <b>))
(list (alpha b "string") "alpha b"))
(alpha (make <b>))
;; -> (("alpha a" "string") "alpha b")
@end example
--
Marco Maggi
"They say jump!, you say how high?"
Rage Against the Machine - "Bullet in the Head"
_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: contrib: goops doc: calling next-method
2006-03-07 22:36 Marco Maggi
@ 2006-03-11 12:16 ` Neil Jerram
0 siblings, 0 replies; 3+ messages in thread
From: Neil Jerram @ 2006-03-11 12:16 UTC (permalink / raw)
Cc: guile-user
"Marco Maggi" <marco.maggi-ipsu@poste.it> writes:
> Ciao,
>
> I propose the following to be appended to the
> Next-method node in the GOOPS tutorial Texinfo.
Thanks. I think I understand your concerns, but the text you have
proposed seems a little long-winded and insufficiently explicit about
the background of your concerns.
What do you think about the following revision of the Next-method
node? Does it cover everything that you wanted to cover?
Neil
@node Next-method, Example, Generic functions and methods, Generic functions
@subsection Next-method
When you call a generic function, with a particular set of arguments,
GOOPS builds a list of all the methods that are applicable to those
arguments and orders them by how closely the method definitions match
the actual argument types. It then calls the method at the top of this
list. If the selected method's code wants to call on to the next method
in this list, it can do so by using @code{next-method}.
@lisp
(define-method (Test (a <integer>)) (cons 'integer (next-method)))
(define-method (Test (a <number>)) (cons 'number (next-method)))
(define-method (Test a) (list 'top))
@end lisp
With these definitions,
@lisp
(Test 1) @result{} (integer number top)
(Test 1.0) @result{} (number top)
(Test #t) @result{} (top)
@end lisp
@code{next-method} is always called as just @code{(next-method)}. The
arguments for the next method call are always implicit, and always the
same as for the original method call.
If you want to call on to a method with the same name but with a
different set of arguments (as you might with overloaded methods in C++,
for example), you do not use @code{next-method}, but instead simply
write the new call as usual:
@lisp
(define-method (Test (a <number>) min max)
(if (and (>= a min) (<= a max))
(display "Number is in range\n"))
(Test a))
(Test 2 1 10)
@print{}
Number is in range
@result{}
(integer number top)
@end lisp
(You should be careful in this case that the @code{Test} calls do not
lead to an infinite recursion, but this consideration is just the same
as in Scheme code in general.)
_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: contrib: goops doc: calling next-method
@ 2006-03-12 6:09 Marco Maggi
0 siblings, 0 replies; 3+ messages in thread
From: Marco Maggi @ 2006-03-12 6:09 UTC (permalink / raw)
Neil Jerram wrote:
> What do you think about the following revision of
> the Next-method node? Does it cover everything
> that you wanted to cover?
Yes. I would not exclude the examples though.
I often have problems with words, no matter how clear
they are; with enough examples I can understand 80%
almost immediately.
Examples is what I look for in tutorials; maybe
other novices (the tutorial audience) on this list
may step in and say what they think.
Thank You.
_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2006-03-12 6:09 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-03-12 6:09 contrib: goops doc: calling next-method Marco Maggi
-- strict thread matches above, loose matches on Subject: below --
2006-03-07 22:36 Marco Maggi
2006-03-11 12:16 ` Neil Jerram
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).