[Openmcl-devel] defstruct - generic constructor?
rpgoldman at sift.info
Wed Apr 29 22:26:17 UTC 2009
David Reitter wrote:
> On Apr 29, 2009, at 5:53 PM, Robert Goldman wrote:
>> "defstruct defines a constructor function that is used to create
>> instances of the structure created by defstruct. The default name is
>> make-structure-name. A different name can be supplied by giving the name
>> as the argument to the constructor option. nil indicates that no
>> constructor function will be created."
> That is one of the reasons why I'd like to avoid the hack...
>> Question: are all of the structures that you might want to make under
>> your control?
> I'd have to mandate make-* constructors in my API. Not ideal.
>> As an aside, it seems likely that (intern <name> <package>) will be more
>> efficient than read-from-string, and it won't be busted by changes to
>> *package* in the surrounding environment.
> I tried that first and failed. I just figured out that I had to use
> upper case "MAKE-..."; with `find-symbol' in this case.
> Less of a hack now. Good.
> Dan Weinreb:
>> This behavior isn't specific to CCL; it's part of the definition
>> of Common Lisp. If you want to use make-instance, you
>> should use defclass instead of defstruct. (defstruct is an
>> earlier, more primitive facility that was added to Lisp
>> before object-oriented programming.)
> Another good idea; I'll definitely consider doing that, with a macro to
> allow users of my API to define their classes using an interface that is
> as simple as defstruct.
> It seems that there are much better facilities for introspection
> available for classes than for structures.
If you are going to force people to use your macro, you could have the
macro squirrel away information about the constructor... And you could
enforce the existence of a constructor that would tolerate no args.
Question: how important is the performance of these structures? Using
classes would be easier, unless you want the greater performance of
structure slot accesses.
I'm looking at the MOP and I can't see any portable way to extract the
information you want from STRUCTURE-CLASS but someone out there who
knows the MOP better than me might be able to figure out how to do this.
More information about the Openmcl-devel