4 ms·
I mostly liked this article to show some of the basic features of CLOS, but was a little disappointed about the attitude toward the end. The attitude toward som
by reikonomusha 11y ago
I mostly liked this article to show some of the basic features of CLOS, but was a little disappointed about the attitude toward the end. The attitude toward some of the more advanced features of CLOS like method combination reminds me of the attitude that some people have for functional programming. "Well, it's cool you can 'map' and 'reduce' a function across a list, but why not just use a for-loop?" I think most people on HN can see why that may not be a good attitude to have because of the larger implications of functional programming.
Method combination has an overarching theory behind it. My take is as follows: Methods are by definition associated with a set of constraints on the arguments. The constraints are, for the most part, specified by their class. Because classes form a mathematical lattice, we have the notion of "more and less specifically applicable methods" for a set of arguments, and so the aforementioned constraints can be ordered and usually the most specific method is called, as the article explains. But the key thing to notice here is that if we remove the notion of "more or less applicable", and replace it with just the notion of "applicable", then we can do some interesting things.
For a particular generic function (Lisp parlance for a method with unspecified constraints), and for a given set of arguments to the function, there is a set of applicable methods. How we arrange to execute these applicable methods, and how we arrange to use the results of the executions, is what constitutes method combination.
There are two trivial ways to arrange what gets executed. The popular way is to just execute the most specific only. The other trivial way is to just execute everything. Many kinds of non-default method combinations (like the LIST method combination) do this. Another one, which I find generally more useful, is the + method combination. (This sums all of the results of the applicable methods. One usual example is calculating prices with additive taxes.)
Many problems can be decomposed into one which sounds like the following: 1. Classify your objects into a (multiple-inheritance) class hierarchy. 2. Write down the behavior that is applicable to products of these objects.
If you can do 1 & 2, then these aspects of CLOS and method combination can solve your problem in a clean way.
* * *
There were a few nits I had with the article otherwise. What he calls "forms" are not forms. "Forms" are memory representations of the syntax. The syntax is called "symbolic expressions" or "S-expressions" or "S-exprs". An even more pedantic nit is that IF is not called a "special form", it is called a "special operator". The combination of a special operator and its arguments make a "special form". (But who cares. Even seasoned Lispers say "special form" for operators.)
- arethuza 11y agoIndeed, where the article says: "these features are too advanced to be useful in 99% of the cases" I'm pretty sure when I wrote CLOS for a living (mind you, not recently) that I used :before, :around and :after rather more than that - they are a very natural and pretty simple extension to the basic method dispatch mechanism.
- Avshalom 11y agoI mean they are if nothing else a good place to shove sanity-checking/design-by-contract code with out bloating (see: making harder to reason about) the actual method.
- kbenson 11y agoSince ~2009 Perl 5 has had the Moo* ecosystem of modules (Moose, Moo, Mouse) to provide an object system that supports much of this. It was created as a backport of ideas for Perl 6's object system, which was heavily inspired by CLOS and the Smalltalk traits paper (always liberally steal the best ideas!). That is, there's quite a lot of Perl 5 out there that makes extensive use of method modifiers[1] like before, after, around and friends. 1: http://search.cpan.org/dist/Moose/lib/Moose/Manual/MethodModifiers.pod http://search.cpan.org/dist/Moose/lib/Moose/Manual/MethodMod...
- GregBuchholz 11y agoI wonder if anyone has a favorite example that uses method combinations, or auxiliary methods (:before, :after, :around), or something MOP related. Preferably one that is "in-the-wild" and not invented just to showcase the feature.
- reikonomusha 11y agoThere are lots of examples of auxiliary methods. Here's an example of using around-methods to add slot-caching logic to a project for formula rendering: https://bitbucket.org/tarballs_are_good/formulador/src/639107f9266d5c4c2c679390f89153fe449bc201/boxes.lisp?at=default&fileviewer=file-view-default#boxes.lisp-60 https://bitbucket.org/tarballs_are_good/formulador/src/63910...