Programmatically name a struct

Discussion of Common Lisp
Post Reply
mgcheung
Posts: 5
Joined: Tue Jun 26, 2012 6:21 am

Programmatically name a struct

Post by mgcheung » Tue Jun 26, 2012 7:15 am

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?

wvxvw
Posts: 127
Joined: Sat Mar 26, 2011 6:23 am

Re: Programmatically name a struct

Post by wvxvw » Tue Jun 26, 2012 1:51 pm

Not sure if I understood you correctly, but

Code: Select all

(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?

mgcheung
Posts: 5
Joined: Tue Jun 26, 2012 6:21 am

Re: Programmatically name a struct

Post by mgcheung » Wed Jun 27, 2012 6:42 am

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

Post Reply