[Openmcl-devel] defstruct - generic constructor?

Pascal Costanza pc at p-cos.net
Thu Apr 30 04:34:33 PDT 2009


Common Lisp is a higher-order language. So:

(defstruct foo a b c)
(defstruct bar x y z)

(defun make-something (instance-maker)
   (funcall instance-maker))

(make-something #'make-foo) => #S(FOO :A NIL :B NIL :C NIL)

(make-something #'make-bar) => #S(BAR :X NIL :Y NIL :Z NIL)

You can also apply the make-xyz functions to some list of  
initialization arguments.

I hope this helps.


On 29 Apr 2009, at 22:26, David Reitter wrote:

> I've been trying to make instances of objects whose types have been
> defined using `defstruct'.
> Unfortunately, the obvious `make-instance' doesn't work (with ccl at
> least).  I'm glad, however, that `class-of' and `class-name' do their
> jobs.
> The only way I got this to work was this clumsy hack:
> (apply (read-from-string (format nil "make-~a" (class-name chunk- 
> type)))
> ...)
> Is there a nicer, more efficient way of doing this?
> I'd need to find out what the constructor is for a given class.
> Reading the `defstruct'  code didn't help me much.
> In more general terms, can I expect that `class-of' and `class-name'
> work for structures in other CL implementations?
> Thanks for your input.
> _______________________________________________
> Openmcl-devel mailing list
> Openmcl-devel at clozure.com
> http://clozure.com/mailman/listinfo/openmcl-devel

ELS'09: http://www.european-lisp-symposium.org/

Pascal Costanza, mailto:pc at p-cos.net, http://p-cos.net
Vrije Universiteit Brussel
Programming Technology Lab
Artificial Intelligence Lab
Pleinlaan 2, B-1050 Brussel, Belgium

More information about the Openmcl-devel mailing list