Common Lisp provides two ways to create new compound data types: defstruct
and
defclass
. Defstruct
creates simple cartesian record types, while defclass
is part of a full object-oriented programming system. How do you
decide which one to use?
It’s easy. Unless you have a compelling reason to use defstruct
, just
use defclass
. Even if you don’t use any other features of CLOS, defclass
better supports class redefinition, and this just makes life easier.
If you modify a defstruct
and recompile it, the old instances of that
struct type become obsolete. They probably won’t work with the new
definition. You’ll most likely have to rebuild them. If things get
too screwed up, you’ll end up having to restart your Lisp image.
CLOS, on the othe hard, is designed to be dynamic. You can redefine
and recompile a class on the fly. You can change the class of an
instance. As you develop your code, you’ll be adding and removing
slots and changing the class hierarchy. defclass
usually handles
these sorts of dynamic changes transparently, without having to
restart your Lisp image.
CLOS achieves this by adding an extra level of indirection, and perhaps
you cannot tolerate the extra overhead. Then by all means use
defstruct
. But if you are indifferent,
defclass
is a better choice.
No comments:
Post a Comment