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?
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With