Reputation: 18918
I would like to do something akin to this:
(defstruct person
real-name
(fake-name real-name)) ;if fake-name not supplied, default to real-name
However, Common Lisp says The variable REAL-NAME is unbound.
So how can I get the constructor to evaluate its arguments sequentially (like I can with function keyword arguments), or how else should I be better doing this?
Upvotes: 5
Views: 949
Reputation: 31053
One way is:
(defstruct (person
(:constructor make-person (&key real-name
(fake-name real-name))))
real-name
fake-name)
You can essentially tailor the constructor function to your needs, including
make-xxx
Consider
(defstruct (person
(:constructor make-person (real-name
&optional (fake-name real-name))))
real-name
fake-name)
You can even initialize constructed fields using the &aux
lambda-list keyword:
(defstruct (person
(:constructor make-person (real-name
&aux (fake-name (format nil
"fake-of-~A"
real-name)))))
real-name
fake-name)
Upvotes: 11