Search code examples
common-lispdefault

Is it possible to change Common Lisp's `getf` function default value to something else than NIL?


The function getf works like that:

CL-USER> (getf '(:name "pedro") :name)
"pedro"
CL-USER> (getf '(:name "pedro") :whatever)
NIL

NIL is the default value. Is it possible to change it?


Solution

  • Yes. The documentation defines this possibility:

    (getf place indicator &optional default)
    

    Thus, the default value it's actually an optional argument. An example using it would be:

    
    CL-USER> (getf '(:name "pedro") :name "no-answer")
    "pedro"
    CL-USER> (getf '(:name "pedro") :whatever "no-answer")
    "no-answer"