Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between struct and class in Common Lisp

My understanding of a struct is that it has slots to store data in, has a type, has make-X and slot-accessor functions, and can be specialized on by a method (since it has a type).

My understanding of a class is that it has all of the same and multiple inheritance. The top answer to this question states that structs can have single inheritance, and that initial implementations of CLOS were "much slower" than structs.

Based on how people talk about CLOS and structs, I assume there must be some other differences, but my trivial google searches have been fruitless. So I ask: what are the practical differences between CLOS and structs?

like image 757
Reepca Avatar asked Dec 12 '15 20:12

Reepca


2 Answers

Structures

Structures are more primitive. They provide what often is called a record in programming languages. They were invented before classes. CLtL1 (the first book describing Common Lisp) in 1984 already had structures and later a standard object system called CLOS was added. Structures provide

  • a concise definition macro DEFSTRUCT
  • single inheritance
  • fast slot access
  • defines reader and setf access for slots
  • defines a type predicate
  • defines a constructor function
  • defines a copy function
  • a printed representation: structures can be read and printed
  • above functions maybe inlined

useful additions:

  • DEFSTRUCT can define list- and vector-based representations of structures, in addition to structure types

Limitations:

  • after a change to a structure type, structure instances are not updated
  • if a structure type is changed, it is best to recompile and re-run changed code. Maybe restart the program. The effects of redefining a structure are undefined in standard Common Lisp.
  • very little introspection: portable Common Lisp does not tell me super/sub-strucures of a structure in an easy way. portable Common Lisp does not tell me the slots of a structure.
  • no slot access via slot names by default

Extensions

  • some implementations provide more runtime introspection and a little bit integration in some CLOS functions

CLOS classes

CLOS was invented in the mid/late 80s based on two earlier object systems (Flavors and LOOPS). It provides:

  • a defining macro DEFCLASS
  • multiple inheritance
  • protocols for creation, initialization, etc.
  • CLOS objects can be changed and updated at runtime based on class changes (new slots, redefined slots, removed slots, changed inheritance, ...)
  • CLOS objects can change their class and be updated at runtime
  • access via slot names possible

limitations:

  • no default printer/reader
  • DEFCLASS definitions are not very concise

extensions

  • faster slot access by added implementation specific functionality
  • Meta-Object Protocol provides added functionality and flexibility: introspection and reflection. Sometimes only parts of the MOP are provided.
  • user-provided extensions are available, especially for implementations with MOP support

Common Lisp

In some cases the Common Lisp standard does not say how a functionality should be implemented: structures, classes, or maybe even something else. Examples are streams and conditions. It's usually a good sign (for added flexibility) if a Common Lisp implementation uses CLOS for those.

like image 178
Rainer Joswig Avatar answered Oct 27 '22 14:10

Rainer Joswig


defstruct does more work for you behind the scenes, e.g.:

  1. defines slot accessors automatically
  2. defines readable print-object method

Also, structure slot access is faster (although the difference is probably inconsequential).

The bottom line is that, unless you need MOPish features, you can get away with defsrtuct.

like image 24
sds Avatar answered Oct 27 '22 12:10

sds