This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

Operator Overloading

3 posts · 8826 views

Is there an easy way to overload operators/functions in Emacs Lisp?

I'd like to overload + to handle custom-defined data types, other than "normal" numerics.

What I'd like to do is write a procedure that checks the types of the arguments, and if they're my custom-defined types, then perform the custom defined operations I define, and otherwise return handling to the "normal" implementation of +.

Re: Operator Overloading

You probably know this by now, but as + is a primitive function implemented in C, it is not wise to try to redefine or advise it. Though normal functions will see the new definition, other primitive functions will still call the underlying + directly.
Dogs and cats, living together.
Parenthetically speaking, that is.

Re: Operator Overloading

Since your procedure has different semantics than the built-in +, it probably deserves a new name. (What if two libraries decide to extend + in incompatible ways?)

With this new function name, it is trivial to dispatch back to the built-in + where appropriate.