I am using Common Lisp for developing a web application. Several days ago a new part of this application didn’t worked as supposed and I spent a considerable large amount of time in finding the bug. It was a very simple problem with symbols where I mixed something up.
In the application the web server somewhen gets some JSON data from
the browser. It is then converted to Lisp object using the
package. This package converts JSON objects to a-lists and converts
the member keys to symbols (see CL-JSON’s documentation. I then wanted to look something up in that a-list and failed.
I wrote a small test case to show the effect and explain what went wrong.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
So on the REPL everything looks fine:
1 2 3 4 5
But when I used my web browser to give me these results as well I got something strange. For example here are some results when using curl:
1 2 3
I was puzzled: The value is
After some debugging I found out that the easy handler from
Hunchentoot runs with
*package* set to
(and not to
MY-APP as I implicitly assumed). That means that
assoc looked up
COMMON-LISP-USER::FOO in the a-list where
the keys are
And this test fails. Therefore
NIL is returned which is correct.
So I rewrote the
get-value function to:
1 2 3 4
Now the symbols are interned in the same package and everything went well:
1 2 3 4 5 6 7
Therefore I was reminded to think about packages when interning symbols. A good guide to symbols and packages could be found in this document: The Complete Idiot’s Guide to Common Lisp Packages.