Operator Overloading

Discussion of Emacs Lisp

Operator Overloading

Postby psholtz » Wed Aug 17, 2011 5:24 pm

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 +.
psholtz
 
Posts: 5
Joined: Sun May 01, 2011 1:41 pm

Re: Operator Overloading

Postby spacebat » Sun Aug 19, 2012 4:33 am

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.
spacebat
 
Posts: 3
Joined: Sun Jan 10, 2010 2:49 am
Location: Adelaide, South Australia

Re: Operator Overloading

Postby nuntius » Wed Aug 22, 2012 9:08 am

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.
User avatar
nuntius
 
Posts: 497
Joined: Sat Aug 09, 2008 10:44 am
Location: Burlington, MA


Return to Emacs Lisp

Who is online

Users browsing this forum: No registered users and 1 guest

cron