1

Is there any way to specify a slot-option for a struct slot without having to give it a default value? This can be of benefit e.g. when defining a base structure from which other structures would inherit, specifying the :types or :read-only without giving an initial value would look nicer i think.

The following structure definition leads to an error since the initial value is missing:

(defstruct s
  (x :type number :read-only t)
  )
Student
  • 708
  • 4
  • 11

1 Answers1

3

No. One (of many) difference between defstruct and defclass is that all structure slots always have a value, and not specifying the default is equivalent to specifying nil default.

Note that

The restriction against issuing a warning for type mismatches between a slot-initform and the corresponding slot's :type option is necessary because a slot-initform must be specified in order to specify slot options; in some cases, no suitable default may exist.

In your case, I recommend:

(defstruct s
  (x (error "x is required") :type number :read-only t))

in which case (make-s) will signal an error, while (make-s :x 10) will work.

sds
  • 58,617
  • 29
  • 161
  • 278