Define Alternatives

Marcin Borkowski (mbork) has an excellent post concerning a function I didn’t know about. It helps with the following problem: suppose you have a function with several implementations, want to access them with a single function name, and need to choose the implementation to use when you invoke the common function name.

That probably seems a little obscure but the example that mbork gives makes it clear. His use case is wanting to play some music using one of mplayer, vlc, or mpv using a single invocation name.

There are plenty of straightforward ways of doing this, of course, but mbork shows a very nice builtin method: define-alternatives. It’s easy to use and set up. You simply provide an alist of method/function pairs and use define-alternatives to specify a function name that will invoke the method of the user’s choice. See mborks post or the documentation for the details.

Define-alternatives is a great way of handling this situation. It’s hard to imagine a method that requires less code or effort on the developer’s part. As mbork says, the method doesn’t seem to be well known so it’s worthwhile pointing to mbork’s post as a way of getting the word out.

This entry was posted in General and tagged . Bookmark the permalink.