Issue
I can use getf to extract values inside a list:
CL-USER>(defvar regular-list-keys '(:name "pedro" :value "2985"))
REGULAR-LIST-KEYS
CL-USER> (getf regular-list-keys :name)
"Pedro"
CL-USER> (getf regular-list-keys :value)
"2985"
Ok. I was expecting the same to happen when dealing with |:keywords|:
CL-USER> (defvar odd-list-keys '(|:name| "jazoest" |:value| "2985" |:type| "hidden"))
ODD-LIST-KEYS
CL-USER> (getf odd-list-keys :name)
NIL
CL-USER> (getf odd-list-keys |:name|)
error
Why does this happen? And how can I solve this?
Obs.: I can change previous work which is returning data with |:foo| format if it is necessary.
Solution
There's two problems.
First, escaping a symbol prevents case folding. So with the pipes, you get lowercase symbols, rather than the default uppercase symbols (unless you've modified (readtable-case *readtable)).
Second, putting : inside the pipes makes it a literal character in the symbol name, not the keyword package prefix. So you're creating an ordinary symbol in the current package, not a keyword.
:name is a symbol named "NAME" in the KEYWORD package. |:name| is a symbol named ":name" in the CL-USER package.
Symbols that aren't in the KEYWORD package don't automatically evaluate to themselves. So you need to quote |:name|. This will work:
(getf odd-list-keys '|:name|)
Answered By - Barmar Answer Checked By - David Goodson (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.