This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

Programmatically name a struct

3 posts · 2370 views

I was wondering if there was a way to create a new struct where the name of the struct is created programmatically. The idea is there is a constant part of the name and then a part of the name the user could specify. My thought would be to do something like (defstruct (symb 'const name) x y), but I know that doesn't work. Is there something like symbol-function for structs?

Re: Programmatically name a struct

Not sure if I understood you correctly, but
(defmacro defstruct-x (name &rest rest)
  `(defstruct
       ,(intern
         (concatenate 'string "FOO-"
                      (symbol-name name))) ,@rest))

(defstruct-x person (name "John" :type string))

(make-foo-person :name "James")
would do it?

Re: Programmatically name a struct

Thanks! The only change I made was to use the symb function from On Lisp since I had that code already.