How can I make the constructor of a structure evaluate sequentially in Common Lisp?

I would like to do something similar 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 (for example, I can with the arguments of the function keyword), or how am I better to do this?

+5
source share
1 answer

One of the methods:

(defstruct (person
             (:constructor make-person (&key real-name
                                             (fake-name real-name))))
  real-name
  fake-name)

You can essentially customize the constructor function to your needs, including

  • with a different name than make-xxx
  • with Lisp, create a constructor "in order of argument" (BOA) instead of the keyword

Consider

(defstruct (person 
             (:constructor make-person (real-name
                                        &optional (fake-name real-name))))
    real-name
    fake-name)

, &aux -:

(defstruct (person
             (:constructor make-person (real-name
                                        &aux (fake-name (format nil
                                                                "fake-of-~A"
                                                                real-name)))))
    real-name
    fake-name)
+10

All Articles