What about CLOS in clojure?

I’m sure lots of people have toyed with the idea of implementing a CLOS-like library in Clojure in user space. I found clj-clos but it looks like it has not been touched since 2013. Does anyone know whether the author David Bergman is still active? Is the project abandoned?

I recently implemented a very complicated set of functions which would have been much simpler had I had access to CLOS, and in particular method combinations. Things like this are never essential, as a determined programmer can implement lots of things, but sometimes is not to use existing, well-understood abstractions and not re-invent the wheel.

I have been tempted to refactor my code to implement just the part of CLOS that I need to make my particular method combination work. But perhaps it would be more useful to contribute to an existing CLOS-in-clojure effort if such exists.

1 Like

Maybe this project implements what you’re looking for (incl before/after/around)

2 Likes

Thanks bsless, that looks interesting. The author has defined several method combinations, but it is unclear whether new method combinations can be added in user space, or whether I have to edit the code for the methodical code to add a new one.

The method combination that I need is one that sorts the methods by a topological sort before launching the first one, then starts calling the methods in turn until one returns true or false, and which time that value gets returned and the calling chain terminates. I.e., each of the multimethods decides whether it knows how to compute the result, and if so, returns the result otherwise declines to answer, and the next method gets called.

I can also do this less elegantly with the standard method combination. Each would return true, false, or (call-next-method).

From a cursory glance at the implementation, maybe you can do it by implementing the MethodCombination protocol yourself. But you can ask the library author about the best way to go about it, or maybe even a feature request for a way to add new combinations.

1 Like

It appears the library is maintained, the most recent commit was 13 days ago.

It is maintained and the author replies fairly quickly to issues. You can add your own method combinations. It also includes a few standard ones like CLOS does. It also allows method redefinition without restarting your program which Clojurescript built-in multi-methods don’t. It explains how it does so in the docs. It also does method lookup faster than the normal Clojure version via a cache strategy, which you can also provide your own implementation for. You may want that for you topological sorted methods. It has AND and OR method combinations that short circuit as well. You can implement your own dispatcher as well. The library is very flexible and you can do what you describe.

3 Likes