atlas-engineer / nclasses

A `define-class` macro for less boilerplate
Other
7 stars 2 forks source link

Inline documentation (slot-name initform "docstring") #2

Open aartaka opened 1 year ago

aartaka commented 1 year ago

I've been bringing this up here and there, but here's a PR.

I believe it would be an aesthetic and useful change allowing inline documentation in our custom syntax for class definitions, like

(class-star:define-class foo ()
  ((slot-a
    5 "Some number.")
   (slot-b :unbound "Initially unbound slot.")))

This is going to cut some lines of code for short slots and make long slots slightly easier to follow when used to.

But, obviously, it causes quite some trouble for slot parsing, which is already quite complex.

If we agree that this is something we want, I'm ready to prototype this thing with all the caution left in me :P

Ambrevar commented 1 year ago

Agree it's a nice thing :)

I wonder if it's possible though. Take a slot that has the :reader initform.

(malignant-slot :reader "My docstring")

Kaboom! :p

aartaka commented 1 year ago

Yes, but for such cases one's better off using a regular :initform/:documentation syntax because it's ambiguous. We can't provide any guarantees for such use :P

Other than that extreme corner-case, the thing is relatively doable with e.g. pattern matching.

Ambrevar commented 1 year ago

Let's not introduce a library for just this, it's easy enough to check if the 3rd element is a string and the second is not a keyword.

I think the following check would work:

(and
  (oddp (length DEFINITION))
  (not (keywordp (nth 1 DEFINITION)))
  (stringp (nth 2 DEFINITION))
Ambrevar commented 1 year ago

Alternatively, check that the second element is not in

(append '(:accessor :initarg :initform) *allowed-slot-definition-properties*)

aartaka commented 1 year ago

Yes, fair.