Page 1 of 1

Operator Overloading

Posted: Wed Aug 17, 2011 5:24 pm
by psholtz
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

Posted: Sun Aug 19, 2012 4:33 am
by spacebat
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.

Re: Operator Overloading

Posted: Wed Aug 22, 2012 9:08 am
by nuntius
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.