Programmatically name a struct

Discussion of Common Lisp

Programmatically name a struct

Postby 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?
mgcheung
 
Posts: 5
Joined: Tue Jun 26, 2012 6:21 am

Re: Programmatically name a struct

Postby 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?
wvxvw
 
Posts: 127
Joined: Sat Mar 26, 2011 6:23 am

Re: Programmatically name a struct

Postby 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.
mgcheung
 
Posts: 5
Joined: Tue Jun 26, 2012 6:21 am


Return to Common Lisp

Who is online

Users browsing this forum: No registered users and 3 guests

cron