r/Common_Lisp • u/RentGreat8009 • Jan 01 '22
Why not to overload functions in Common Lisp
https://ashok-khanna.medium.com/why-not-to-overload-functions-in-common-lisp-d7b31065032d2
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
1
1
Jan 09 '22
Well what do you think generic functions are? Who the heck cares about the bogus compile time distinction really?
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.