Why not to overload functions in Common Lisp

  • Lambda Lists: Lambda lists and the ability to specify &optional and &key parameters (and in particular, suppressing keyword argument checking through &allow-other-keys) can be (ab)used to implement function overloading
  • Generic Functions: Generic functions allow us to implement multiple methods for a given function and in a sense overload them as long as the methods share congruent lambda lists

Avoiding Indirection and Fighting Against the Language

But what about Generic Functions? They also introduce indirection!

  • First, CLOS and how it is implemented is part of the ANSI Common Lisp Standard and should be well understood by any experienced Lisp programmer. Thus, it will be easier for us to parse the indirection caused by CLOS generic functions as we are used to it.
  • In addition, CLOS and MOP provide significant tools to introspect generic functions, again helping overcome the layer of indirection introduced by generic functions. An equivalent set of tools do not exist for custom user-defined function overloads.
  • Despite both of these points, it is important to note that generic functions are not meant to be used to overload disparate functions within the same function name. Indeed, the fact that lambda lists must be congruent points to this direction.
  • Rather, generic functions and CLOS in general represent an organisational tool to collect associated concepts in objects and generic functions. It is commonly stressed that generic functions should not be used for function overloading, but rather to represent a single process at a higher level of abstraction.
  • To this point, Sonya Keene in Object-Oriented Programming in Common Lisp notes that the documentation string of a generic function should cover the overall purpose of the function, what it does and what it returns at a high-level.
  • In some ways, this doc string helps reduce some of the indirection caused by generic functions as it summarises the purpose of the function without requiring the reader to determine which method to read to gain this understanding.

Function Overloading is Not a Good Idea

  • Draw a shape
  • Draw a conclusion
  • Draw from a bank account

Packages Exist to Manage Namespace Conflicts

Concluding Remarks

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ashok Khanna

Ashok Khanna

Masters in Quantitative Finance. Writing Computer Science articles and notes on topics that interest me, with a tendency towards writing about Lisp & Swift