Generic lisp "overloading" of built-in functions in a portable and efficient way?
For example, if I want to define new arithmetic operations for vectors or quaternions, etc. I did something like (defun v+ (&rest vectors) ...)
.
Is there a good way to overload the normal one +
? (I only know shadow-import, which doesn't seem to be the best solution)
And if I use +
it will take longer to determine the type of operation.
Is this better than overloading and using different function names and defining a new function only when needed?
source to share
What you probably want to do is define a package (name it my-math
) where you define a character my-math:+
that makes whatever dispatch you want.
Alternatively, define my-math:+
it efficiently (reduce #'my-math:binary+ args)
and then define it my-math:binary+
as a generic function that you can then insert into certain methods.
This takes extra discipline when writing the package definition (s) based on my-math
, as you need to do your best to keep your unpainted +
from my-math
, not, cl
and it might make the code more difficult for another person to read.
source to share