Package canon-initargs
Description
This package is also nicknamed canonicalized-initargs
.
All symbols from this package should normally be explicitly qualified, such as canon-initargs:standard-class
.
Don't (:use)
!
canonicalized-initargs
provides a :canonicalize slot option
accepting an initarg canonicalization function.
Traditionally, there has been no standard way to canonicalize slot values. For instance, you might declare a slot as having type string, but prefer to let the user specify any string designator. In this situation, there are various suboptimal approaches you could take:
Give up and let the user specify a string designator and store that value directly, coercing it to a string when reading the slot's value. This is semantically nauseating and inefficient, and could compromise correctness and the debugging experience.
Give up and force the user to specify the canonicalized value (a string). This is maximally convenient for the implementor, but fairly inconvenient for the user.
Let the user specify a string designator, and correctly canonicalize it to a string internally before storing it in the slot. This is maximally convenient for the user, but fairly inconvenient for the implementor, especially as there is no idiomatic way to do this. It took me 10+ years to find the best way to do this without infrastructure, and it's still fairly inconvenient for the implementor. This could be made easier with a macro, but it's still semantically suboptimal. It would basically be a design pattern, and as Alan Kay said, Design patterns are for when you run out of language
. It can also be a great burden to properly document the specific canonicalization strategy you chose, especially as canonicalization is incredibly often desirable.
The complete lack of built-in support for slot value canonicalization (or even of a standard idiom for it) might be seen as one of the biggest unacknowledged blind spots in the Common Lisp language. (Although most programming languages probably share that limitation.)
canonicalized-initargs implements the platonically ideal solution using the MOP. You can just pass :canonicalize #'string
as a slot option and the corresponding initarg will be canonicalized! There is also a :canonicalize
class option (similar to :default-initargs
) to canonicalize initargs not necessarily associated with slots.
This is a very intuitive and broadly useful killer app for the MOP!
This library is currently undocumented, sorry. See note on Libraries page.
This package is also nicknamed canonicalized-initargs
.
All symbols from this package should normally be explicitly qualified, such as canon-initargs:standard-class
.
Don't (:use)
!