[pro] Modularity for subclassing in Common Lisp

Faré fahree at gmail.com
Wed Dec 1 17:16:22 UTC 2010


On 1 December 2010 10:25, Daniel Weinreb <dlw at itasoftware.com> wrote:
> I call the set of defgenerics (plus the factory functions) the
> "protocol".  The word "type" is sort of right but carries a lot of
> connotations and freight that I'd rather avoid.
>
Here is a way that CL sucks badly: protocols are not first-class entities.
To modify a protocol is something done to the source code *outside* of the
Lisp world. It cannot be done programmatically.
Adherence to the protocol cannot be enforced.
Discrepancies cannot be detected.

In Racket, for instance, modules are first-class (at syntax expansion time)
and units are first-class (at runtime), and you can manipulate them
programmatically.

> First, a common base class can provide implementations of some of the
> generic functions all by itself.  My favorite simple example is an
> "output stream" protocol, that has a write-character operation and a
> write-string operation.  The common base class provides an
> implementation of write-string that works by iterating over the
> characters of the string and calling write-character.  Any output
> stream that can write strings in a more efficient way can override
> that method.
>
In my "pure" datastructure library (currently part of fare-utils,
to be spun off as lil - lisp interface library), I use mixins to
provide these "methods". So instead of adding the method to a base class,
I would provide a mixin "write-string-from-write-char", and
then could possibly add an opposite mixin "write-char-from-write-string",
without creating a paradox that will byte you.

> This is not perfect.  A programmer might just happen to create a
> vector and put one of those keywords into the +fhash-kind+ slot.
> (+fhash-kind+ is zero but that's a mere detail.)  But it's sort of
> good enough.
>
Solution: don't use keywords, but private symbols. Unlike keywords,
private symbols are private. They can be faked, but not accidentally.
CL doesn't allow you to easily define sublanguages
in which things cannot be faked. That's painful.

> What people usually do in Common Lisp, in my experience, [...]
Patterns mean "I have run out of language." — Rich Hickey

[ François-René ÐVB Rideau | Reflection&Cybernethics | http://fare.tunes.org ]
If you could kick the person in the pants responsible for most of your
trouble, you wouldn't sit for a month. — Theodore Roosevelt




More information about the pro mailing list