r/Common_Lisp • • Jan 01 '22

Why not to overload functions in Common Lisp

https://ashok-khanna.medium.com/why-not-to-overload-functions-in-common-lisp-d7b31065032d
1 Upvotes

16 comments sorted by

11

u/lispm Jan 01 '22 edited Jan 01 '22

Usually I would think the concept of function overloading is not supported in Common Lisp.

Function overloading usually means associating a function with multiple implementations (with different argument lists or different argument types) AND the ability to resolve this at compile time. Function overloading is done statically: the compiler determines at compile time which function to use.

CLOS generic functions dispatch at runtime on the dynamic class / identity of their arguments. The applicable methods are determined at runtime, ordered and combined to an effective method, which then gets called with the arguments. Thus the general dispatch mechanism in CLOS does not even select a single method, but assembles an effective method based on predefined building blocks using a method combination (which can be specified for each generic function individually).

Methods in CLOS belong to a generic function. Thus one would not see them as independent. One sees them as building blocks for dynamic runtime behavior of that generic function. Thus it makes sense to keep the generic function to be domain specific: printing an object to a stream, drawing a line to a graphic display, ... One then wants to document the general purpose of the generic function with the generic function definition.

This large amount of dynamic behavior makes CLOS code actually easy to read and difficult to understand. Then we need to talk about the meta-object protocol.

3

u/RentGreat8009 Jan 01 '22

Many thanks. I saw a note on resolving function overloads at compile time but didn’t see it on the Wiki page, but I will update now esp in line with the very valuable notes you mention below.

The notes you just mentioned are very helpful and I didn’t see that perspective online just yet in what I was reading. Will add in. Appreciate if you have any more insights to the topic :-)

(I must admit some of the complexity in method combination I want to avoid mentioning to others since its a whole another level of indirection - even though I am using it and it is very enjoyable to use in actual day to day programming.)

3

u/lispm Jan 01 '22

https://en.wikipedia.org/wiki/Function_overloading

Function overloading should not be confused with forms of polymorphism where the choice is made at runtime, e.g. through virtual functions, instead of statically.

Best to read the Wikipedia pages about these topics with some caution. They are not well organized and the content is not very coherent.

There were some useful papers written about CLOS by its designers: https://www.dreamsongs.com/CLOS.html

2

u/RentGreat8009 Jan 01 '22

Thanks!

Indeed, those wiki pages definitely need a clean up by not mentioning something as important as that until down the page.

2

u/moon-chilled Jan 02 '22

I would think the concept of function overloading is not supported in Common Lisp.

Hmm, you can kinda do it with compiler macros though ... :)

4

u/EdwardCoffin Jan 02 '22

You can't rely on compiler macros ever being expanded though, and even if you could, it would be an explicit violation of their intent.

2

u/RentGreat8009 Jan 01 '22

This is a bit in a draft status, but hopefully it proves useful to others who thought about whether to overload functions in Common Lisp. Feedback and discussion most welcome! Please let me know if there's areas you think I may be wrong and if there are other perspectives to consider.

6

u/ForkInBrain Jan 01 '22

After reading your post I am still not sure what you mean by “overloading“ or what precisely it is you’re arguing against. An analogy to C++ doesn’t help me because while I know C++ well I don’t know what you mean when applying it to CL, especially when you then say CLOS generic methods are an example of “overloading” but those are closer to C++ virtual functions, which are orthogonal to C++ overloading concepts. It might be helpful to provide clear definitions, and maybe even code samples.

I did keep thinking that your post might be talking about polymorphism instead of overloading?

2

u/RentGreat8009 Jan 01 '22

Thanks, that’s good feedback. I will update my post with examples and try to make the language more precise.

I was referring polymorphism in Wikipedia and function overloading fell under ad hoc polymorphism there, whilst generic functions fell under parameter polymorphism. However the wiki page for generic functions did not seem suitable at all for CL.

Per Wiki: In some programming languages, function overloading or method overloading is the ability to create multiple functions of the same name with different implementations. Calls to an overloaded function will run a specific implementation of that function appropriate to the context of the call, allowing one function call to perform different tasks depending on context.


Where I was going with generic functions and overloading is that one can abuse generic functions to implement different functions as long as the lambda lists are congruent, e.g. (draw conclusion), (draw shape) and (draw account)

1

u/ForkInBrain Jan 03 '22

I'm not sure if you have gotten a chance to edit your post, but I skimmed it again and still don't understand what you mean by "overloading" in the context of Common Lisp. :-) Disclaimer: I am only passingly familiar with Common Lisp.

I think it would be a good idea to include what you consider to be example(s) of "overloading gone wrong" in Common Lisp, with concrete explanations of the design's drawbacks.

1

u/RentGreat8009 Jan 09 '22

I haven’t had the chance yet, but I wanted to link this article which was very helpful and I will fix up my terminology accordingly: https://dreamsongs.com/Files/clos-cacm.pdf

2

u/ForkInBrain Jan 12 '22

interesting article!

1

u/RentGreat8009 Jan 12 '22

I know right :-)

1

u/cowardly_paper Jan 01 '22

Good read. Thanks for keeping it short and impactful.

1

u/[deleted] Jan 09 '22

Well what do you think generic functions are? Who the heck cares about the bogus compile time distinction really?