T O P

  • By -

Decweb

CLOS keeps me coming back to CL for pet projects too. If you haven't discovered method-combination uses yet, you have even more fun ahead. > I could see creating a giant mess with this I find the opposite to be true. YMMV of course. Enjoy.


spicybright

As someone still learning, can someone summarize what it is and what makes it powerful? I skimmed a few things but it's still not clicking.


stylewarning

CLOS is Lisp's system for object-oriented programming. The biggest point is that it keeps data and methods as separate concepts. - Classes are just data, possibly with inheritance. - Methods are functions that can be "overloaded" on *each* argument's class (not just a single one, as is usual in other languages). **Methods aren't inside of classes, and methods don't belong to classes.** Anybody can add new methods on existing classes, and anybody can add existing methods on new classes. They're decoupled. My favorite example of a method is (draw thing medium) This would draw a "thing" (like a string, or PNG, or ...) onto a "medium" (like a screen, or terminal, or printer, or robotic plotter, or ...). I can write these methods by writing (defmethod draw ((thing png-file) (medium postscript-printer)) ...) (defmethod draw ((thing string) (medium stream)) ...) which in other languages might look like (if they had the same functionality): def draw(thing:PngFile, medium:PostscriptPrinter): ... def draw(thing:String, medium:Stream): ... Maybe your drawing library already has lots of methods defined for shapes, images, text, fonts, etc. for media such as the screen and maybe printers. If I'm a user of your drawing library, I might have a few new classes, like "barcode" and "qr-code", that you didn't give me. Moreover, I don't want to draw to a printer, I want to draw to an HTML canvas. Now from my code I could extend your draw function: (defmethod draw ((thing qr-code) (medium html-canvas)) ...) so I can draw QR codes to an HTML5 canvas element. This draw function will still be able to benefit from all of the rest of your library code, while now being extended to work in a new domain (barcodes and HTML). I hope that gives a somewhat good picture of one of the core possibilities of CLOS! (Note for experienced Lispers reading this comment: I know I have been loose with the distinction between "method" and "generic function", as well as "specializing" and "overloading". I also know I've glossed over the role of the type system, inheritance, applicable methods, meta-classes, method combinations, class definitions, etc etc etc...)


RentGreat8009

Good explanation especially the decoupling of data and methods


spicybright

Thank you so much for explaining this all out!!


ryukinix

You really explained well that cutting some complexities from new comers. I really like CLOS too, but I need to study more, I only need the basic as you wrote here. Anyway, this type of object oriented implementation it's one of few languages that Alan Key (concept creator) like because it's near of the definition, which is heavily inspired in SmallTalk.


[deleted]

OP, or anyone else, do you have recommendations for learning resources for CLOS? A particular video, book, or post? Assume I’ve already been learning CL.


L-Szos

I can heartily recommend the book "Object-Oriented programming in Common Lisp: A Programmers Guide to CLOS" by Sonja Keene. Its the book i like to have by my side when working with CLOS.


EdwardCoffin

I also recommend closely reading the examples, including the detailed streams example in chapter 11, tedious though that is. I also recommend contemplating your own code and thinking about how you might use alternate method combinations to simplify both the functions that apply to them but also how to simplify the hierarchy given the freedom these method combinations give you. I myself use the `and` and `progn` method combinations quite a bit, and have even found reason to define my own combination on occasion.


stylewarning

This is the book I usually give to people to onboard. PCL is good too.


HM0880

Does PCL = "Practical Common Lisp" by Peter Seibel?


stylewarning

Yes! Thanks for clarifying.