3 ms·
TXR is not just a pragmatic, low-dependency, small-footprint tool for hackers, but a bit of a Lisp research platform. For instance, TXR Lisp doesn't have keywo
by kazinator 7y ago
TXR is not just a pragmatic, low-dependency, small-footprint tool for hackers, but a bit of a Lisp research platform.
For instance, TXR Lisp doesn't have keyword parameters "natively". But via the parameter list macro mechanism[1], a function can take keyword params, if the symbol :key appears as a left item in its parameter list. That triggers the :key parameter macro.
TXR Lisp also has a defset, very similar to CL's defsetf. That provides one of the ways by which we can make a form into an assignable place.
Now, here is the cool part. defset[2] and the :key[3] parameter macro do not know anything about each other. Yet if you write a defset for a form that takes key arguments, it works!
Quick demo:
1> (defun f (:key x -- key1 key2) (list x key1 key2))
f
2> (defset f (:key x -- key1 key2) val ^(f-set ,x ,key1 ,key2 ,val))
f
3> (expand '(set (f 1 :key1 2 :key2 3) 42))
(f-set 1 2 3 42)
By contract, the Common Lisp implementations of defsetf I've looked at deal with keyword parameters explicitly. They analyze the parameter list and know which symbols need gensyms and such.
More complex call:
4> (expand '(set (f 1 :key1 (a b) :key2 (c d)) 42))
(let ((#:g0065 (a b))
(#:g0066 (c d)))
(f-set 1 #:g0065
#:g0066 42))
defset ferreted out, through the :key expander, without knowing anything about it, that argument (a b) corresponds to the key1 parameter, and that it needs a gensym, which is then inserted in place of the ,key1 unquote in the ^(f-set ...) backquote template.
[1] https://www.nongnu.org/txr/txr-manpage.html#N-00B4065C https://www.nongnu.org/txr/txr-manpage.html#N-00B4065C
[2] http://www.kylheku.com/cgit/txr/tree/share/txr/stdlib/keyparams.tl http://www.kylheku.com/cgit/txr/tree/share/txr/stdlib/keypar...
[3] http://www.kylheku.com/cgit/txr/tree/share/txr/stdlib/defset.tl http://www.kylheku.com/cgit/txr/tree/share/txr/stdlib/defset...