News for hackers. Stuff that matters.
Re:A polyglot's guide to multiple dispatch – part 3 by eudox on Tuesday April 26, 2016 @12:09PM (#11573052)
To provide an example of the utility of method combinations:

I wrote a project skeleton generator[0] whose functionality is provided by progn-composable plugins, implemented as mixin classes.

Every bit of functionality, like adding a README.md file to a project skeleton, adding a .gitignore file, adding stub unit tests, etc. is implemented as a mixin class. A full template inherits[1] all the mixins it needs.

There is a generic function, render-template[1], that takes a template, some options, and the target directory, and uses the progn method combination. Meaning: every method in the chain is called in succession, and each method adds its own thing to the generated project. The render-template method for the gitignore mixin adds a .gitignore file, for instance. When render template is called on an instance of a class that inherits a bunch of template mixins, it runs all of those mixins in addition to the render-template method of that class itself (if any).

In a sense, this is basically the entity-component pattern[3] implemented in the object system itself, with the caveat that you can't have "multiple instances" of the same component -- you can't inherit a class twice.

[0]: https://github.com/roswell/mystic

[1]: https://github.com/roswell/mystic/blob/2d0fd8e401d50893b4a15...

[2]: https://github.com/roswell/mystic/blob/2d0fd8e401d50893b4a15...

[3]: https://en.wikipedia.org/wiki/Entity_component_system

There are no comments on this comment yet.