Discussion:
Allowing incongruent methods
Eduardo Cavazos
2004-07-05 23:46:55 UTC
Permalink
Hello Goons,

Here's an example of something I'd like to do in a CLOS like language, but
haven't been able to. I'm wondering; does any language out there allow this?
From the GOO manual, I've gathered that GOO may eventually allow this. Also,
since this sort of thing is not yet possible, is there some sort of deep reason
for languages not implementing this capability?

Let's say I want a method called 'range' that yields a list of numbers from 'a'
to 'b' by step 's'. The general form of the method is:

(define-method range (a b s)
(if (> a b)
'()
(cons a (range (+ a s) b s))))

Now I also want a couple of other range methods. One where only 'a' and 'b' are
given and 's' is assumed to be 1. And the case where only 'b' is given, so 'a'
is assumed to be 0 and 's' is assumed to be '1':

(define-method range (a b)
(range a b 1))

(define-method range (b)
(range 0 b))

When I try this in CLOS, Goo, or RScheme, I receive an error.

This is just a simple example to illustrate the idea.

Section 19 of the Goo manual mentions that Goo will in the future allow for
differing numbers of required parameters, so I guess that means that something
like the above will eventually work in Goo.

Thanks for any comments or pointers,

Ed
Chris Double
2004-07-06 00:41:37 UTC
Permalink
Post by Eduardo Cavazos
Here's an example of something I'd like to do in a CLOS like language,
but haven't been able to. I'm wondering; does any language out there
allow this?
Sisc Scheme's object system allows this (http://sisc.sourceforge.net).
The following works fine:

(define-generic range)
(define-method (range (<number> a) (<number> b) (<number> s))
(if (> a b)
'()
(cons a (range (+ a s) b s))))

(define-method (range (<number> a) (<number> b))
(range a b 1))

(define-method (range (<number> b))
(range 0 b))

#;> (range 1 5 1)
=> (1 2 3 4 5)
#;> (range 1 5)
=> (1 2 3 4 5)
#;> (range 1 5 2)
=> (1 3 5)
#;> (range 6)
=> (0 1 2 3 4 5 6)

Is Goo still being maintained?

Chris.
--
Chris Double
***@double.co.nz
Jonathan Bachrach
2004-07-20 16:45:02 UTC
Permalink
Post by Chris Double
Post by Eduardo Cavazos
Here's an example of something I'd like to do in a CLOS like language,
but haven't been able to. I'm wondering; does any language out there
allow this?
Sisc Scheme's object system allows this (http://sisc.sourceforge.net).
(define-generic range)
(define-method (range (<number> a) (<number> b) (<number> s))
(if (> a b)
'()
(cons a (range (+ a s) b s))))
(define-method (range (<number> a) (<number> b))
(range a b 1))
(define-method (range (<number> b))
(range 0 b))
#;> (range 1 5 1)
=> (1 2 3 4 5)
#;> (range 1 5)
=> (1 2 3 4 5)
#;> (range 1 5 2)
=> (1 3 5)
#;> (range 6)
=> (0 1 2 3 4 5 6)
Is Goo still being maintained?
i'm here if that's what you mean. i've been contemplating a minor
release. any suggestions/requests for features and/or fixes?

jonathan
Post by Chris Double
Chris.
--
Chris Double
Eduardo Cavazos
2004-07-18 09:56:14 UTC
Permalink
It turns out that RScheme is able to handle 'incongruent methods' afterall.

Donovan Kolbly (RScheme designer) pointed out to me that a module
called 'rs.sys.multimethod' can be loaded to provide the needed
functionality. Here's the example I gave in my first method, written
to use this module:

,(use rs.sys.multimethod)

(define-mm-generic-function range*)

(define-method range* (a b step)
(if (> a b)
'()
(cons a (range* (+ a step) b step))))

(define-method range* (a b)
(range* a b 1))

(define-method range* (b)
(range* 1 b 1))

PS: Sorry if this is off topic, but in my first message I noted that
RScheme didn't seem to handle this so I wanted to correct my
statement.
Jonathan Bachrach
2004-07-20 16:42:49 UTC
Permalink
we never implemented this in goo. it would be nice. it would probably
be important to add a mechanism to generic function definition to allow
the specification of at least the possibility of optional parameters.
then there's the question of how to address keyword parameters esp wrt
to method selection. in the end we took the conservative route.

jonathan
Post by Eduardo Cavazos
Hello Goons,
Here's an example of something I'd like to do in a CLOS like language, but
haven't been able to. I'm wondering; does any language out there allow this?
From the GOO manual, I've gathered that GOO may eventually allow this. Also,
since this sort of thing is not yet possible, is there some sort of deep reason
for languages not implementing this capability?
Let's say I want a method called 'range' that yields a list of numbers from 'a'
(define-method range (a b s)
(if (> a b)
'()
(cons a (range (+ a s) b s))))
Now I also want a couple of other range methods. One where only 'a' and 'b' are
given and 's' is assumed to be 1. And the case where only 'b' is given, so 'a'
(define-method range (a b)
(range a b 1))
(define-method range (b)
(range 0 b))
When I try this in CLOS, Goo, or RScheme, I receive an error.
This is just a simple example to illustrate the idea.
Section 19 of the Goo manual mentions that Goo will in the future allow for
differing numbers of required parameters, so I guess that means that something
like the above will eventually work in Goo.
Thanks for any comments or pointers,
Ed
Loading...