14

Non-symbols as keyword arguments

 3 years ago
source link: https://lispblog.xach.com/post/631322365781065728/non-symbols-as-keyword-arguments
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
Zach Beane Common Lisp – Non-symbols as keyword arguments

Sometimes I like to have a short function call to make a lookup table with arbitrary keys and values. Something like this:

(table "name" "Alice" "age" 42) => #<TABLE ...>

This is easy enough to define like so:

(defun table (&rest keys-and-values)
   ;;; loop over keys and values to construct a table
   ...)

But there’s another option that works:

(defun table (&rest keys-and-values &key &allow-other-keys)
  ...)

This version has the advantage that missing arguments (e.g. (table "name")) are caught at compile-time!

I’ve been using this construct for a few years, but recently found out about 3.5.1.5, which says:

It is not permitted to supply a keyword argument to a function using a name that is not a symbol.

Yikes! That seems simple and straightforward: what I’m doing is not permitted. However! Every implementation I’ve tried (only three, admittedly) actually allows my not-symbol keywords without complaint!

I’m too old to think that “it works everywhere today” means “it will continue to work in SBCL tomorrow”, so I’m trying to figure out where I stand. 3.5.1.5 also says this:

If this situation occurs in a safe call, an error of type program-error must be signaled unless keyword argument checking is suppressed as described in Section 3.4.1.4.1 (Suppressing Keyword Argument Checking); and in an unsafe call the situation has undefined consequences.

So does that mean my code is:

  • a safe call with suppressed keyword argument checking?
  • …and is that good to use now and forever?
  • an unsafe call with undefined consequences?
  • something else?

I know the same effect could be achieved with a compiler macro, but I’d like to know if I can use this simpler option safely.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK