generic functions to redefine builtin functions - how ? 
Author Message
 generic functions to redefine builtin functions - how ?

Is this any easier in CLOS ?

  I've been futzing around with three different more-or-less
working methods plus quite a few non-working methods before
figuring out that SHADOW was what I needed to be able to redefine
a built-in and inherited primitive within my package:

        ( make-package "GENERIC-SEQUENCES"
                :nicknames '( "GENSEQ" ) :use '(LISP ))
        ( in-package "GENSEQ" )

        ( shadow '( length elt ))

And SHADOWING-IMPORT if I want to make it the default in the USER
package. ( Unfortunately, none of the half-dozen Lisp books I had
on hand went into Packages in any depth, except for CLTL - which
had all of the definitions, and provides a good explaination of
why the things that finally worked *did* work, but wasn't a very
good guide to figureing out what those things might be - i.e. a
typical reference manual. ;-)

I have been working in Xlisp (Xlisp-Stat), which except for a non
CLOS object system, is close enough to Common Lisp, and I've
been cross testing some of my routines in AKCL, to check their
Common Lisp conformance. ( Note that *some* variations of syntax
and defaults equivalent to the above won't be QUITE equivalent in
both Lisp's )

To translate what I've been trying to do into more generic language,
I've been trying to define an object that is 'plug-replacable' for
other sequences like conses and vectors, but that carries along some
extra information and methods. ( XLISP-STAT specifically: extending

[ Perhaps the "ancient and traditional" LISP method to do something
 similar is to use properties, but since properties are properties
 of SYMBOLS and not of values, this doesn't work well for my needs. ]

This redefinition appears to work, except that I've found one instance
where I had to explicitly change "LENGTH" to "LISP:LENGTH" in code
that was loaded after the redefinition - this was when the LENGTH
was part of an expression in the default for a lambda list - for all
instances in the body of the function, the redefined LENGTH seemed to
work properly.

 This interface problem between the built-in objects and class based
objects seems to be a troubled spot in impure/mixed object-oriented
languages ( Well- it is in C++ anyway ... Python's solution works pretty
well. )

  Is this any easier in CLOS ? Do generic functions provide a way to
do this ? ( None of the CLOS books I've looked at provide a hint of
how to do this, although I would think it's a common problem. )

---|  Computer Systems Engineer          University of {*filter*}ia  |---
---|  Department of Molecular Physiology and Biological Physics  |---
---|  Box 449 Health Science Center    C{*filter*}tesville,VA 22908  |---
 [ "The grass is always greener, except at t=0" - Stan Kelly-Bootle ]

Fri, 23 Jan 1998 03:00:00 GMT  
 generic functions to redefine builtin functions - how ?
is it easier in CLOS? well, no.

you can't overload operators like + to accept your defclasses as args.

almost (exactly?) none of the built-in operators are generic functions,
so they can't be overloaded. this is at times a shame, but you can
accomplish the same thing with your own package and then creating your
own defmethods.

that being said, you could do easily as you described and create your
own package, and define your own + function, being careful to call cl:+
when you actually had numbers and only numbers to add.

(defpackage test (:shadow +))

(in-package :test)

  (defclass apple  () ())
  (defclass banana () ())

  (defmethod + ((arg1 apple) (arg2 banana))

  (let ((a (make-instance 'apple))
        (b (make-instance 'banana)))
    (+ a b))

 --> test::ORANGE

(defmethod + ((arg1 number) (arg2 number))
  (cl:+ arg1 arg2)

(+ 3 4)

 --> 7

 -- clint

Sun, 25 Jan 1998 03:00:00 GMT  
 [ 2 post ] 

 Relevant Pages 

1. Builtin methods versus Builtin Functions -- help!

2. Pickler that handles modules, functions, and builtin functions -- done

3. redefining functions using the old function

4. DECIMAL Builtin Function

5. Regina vs IBM REXX: builtin functions

6. How to get the Callable of builtin functions

7. builtin functions

8. i need help on builtin function math

9. filterout() builtin function

10. why is open() a builtin function?

11. Name od builtin functions: is there a rule ??

12. Explicit call of builtin functions


Powered by phpBB® Forum Software