guile-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: More questions regarding generic methods ...


From: Mikael Djurfeldt
Subject: Re: More questions regarding generic methods ...
Date: 19 Mar 2001 00:56:52 +0100
User-agent: Gnus/5.0808 (Gnus v5.8.8) Emacs/20.7

address@hidden writes:

> Ok, i see. I can live with that (for now ;-), i guess what
> i would like to see is some detectable distinction between
> primitives that _can_ be mad into generic functions and
> those that can.

Currently, a <primitive-generic> is actually a procedure, not a
generic function:

(class-precedence-list <primitive-generic>)
 --> (#<<procedure-class> <primitive-generic> 80848c0>
      #<<procedure-class> <procedure> 8084920>
      #<<class> <top> 8083720>)

Since this is work in progress, we don't want to add things to the API
which people might start using.  What is added to the API is the
ability to hang methods on some primitives, and that feature will be
preserved.

Below I've included a list of functions which you can hang methods
on.  That list should be added to the GOOPS documentation.

> On a slightly related topic:
> 
>  *-------------------------------------------------------------------
>  |
>  | guile> (define-method (add (a <string>) (b <string>))(string-append a b))
>  | guile> (generic-function-methods add)
>  | (#<<method> (<string> <string>) 8086f20>)
>  | guile> (define-method (+ (a <string>) (b <string>))(string-append a b))  
>  | guile> (generic-function-methods +)                                    
>  | <unnamed port>:17:1: In procedure generic-function-methods in expression 
> (generic-function-methods +):
>  | <unnamed port>:17:1: Wrong type argument in position 1 (expecting 
> GENERICP): #<primitive-generic +>
>  | ABORT: (wrong-type-arg)
>  | 
> 
> Here it looks like even if a primitive procedure can be made generic
> the predicate GENERICP doesn't return the expected result.

As I've said, a <primitive-generic> is actually a procedure.  If it
were a <generic>, it would have to follow the MOP for method
selection.  It doesn't do that.  It instead first tries to perform its
"normal" behaviour, and then falls back to the collection of methods
which has been hanged onto it.  It is also not possible to produce a
meaningful result for `generic-function-methods', even if we wanted
to.  This is because there is currently no representation in terms of
methods of the "normal" behaviour.

We might want to envision a future where all primitives in fact are
real generic functions, but there are several things that need
improvement before we can consider that.

If you want a kludge which approximates the behaviour you're after,
you can do the following:

  (define-generic generic-function-methods)
  (define-method (generic-function-methods (proc <primitive-generic>))
    (generic-function-methods (primitive-generic-generic proc)))

The selector `primitive-generic-generic' returns the generic function
which holds the fallback methods for the <primitive-generic>.

Note, however, that that method won't return a list of methods which
completely describes the behaviour of the <primitive-generic>.

Best,
Mikael

----------------------------------------------------------------------
It is possible to define "fallback" methods for the following
primitive procedures in Guile:

$abs
$acos
$acosh
$asin
$asinh
$atan
$atanh
$cos
$cosh
$exp
$log
$sin
$sinh
$sqrt
$tan
$tanh
*
+
-
/
<
<=
=
>
>=
abs
angle
ceiling
display
display
exact->inexact
floor
for-each
gcd
imag-part
lcm
magnitude
map
max
min
modulo
negative?
positive?
quotient
real-part
remainder
round
setter
truncate
vector-length
vector-ref
vector-set!
write
write
zero?



reply via email to

[Prev in Thread] Current Thread [Next in Thread]