Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Evaluating: defclass/std #42

Open
vindarel opened this issue Feb 13, 2024 · 5 comments
Open

Evaluating: defclass/std #42

vindarel opened this issue Feb 13, 2024 · 5 comments
Labels
enhancement New feature or request

Comments

@vindarel
Copy link
Contributor

vindarel commented Feb 13, 2024

For an Advent of Code, I was tempted to use the terse defstruct, but they are a pain to use interactively (redefinitions, re-using objects), and also accessing slots programmatically was less easy: no "(slot-value object )".

Anyways. defclass/std offers nice terse macros to define classes.

(defclass/std example ()
  ((slot1 slot2 slot3)))

;; or the shorter
(class/std example 
  slot1 slot2 slot3)

; which expand to:

(DEFCLASS EXAMPLE ()
  ((SLOT1 :ACCESSOR SLOT1 :INITARG :SLOT1 :INITFORM NIL)
   (SLOT2 :ACCESSOR SLOT2 :INITARG :SLOT2 :INITFORM NIL)
   (SLOT3 :ACCESSOR SLOT3 :INITARG :SLOT3 :INITFORM NIL)))

so we get: accessors, initargs, initforms to NIL.

and there are options to control this.

class/std is very similar to defstruct, a difference is that it doesn't create a "example-slot1" accessor by default, it creates the "slot1" accessor, which is a good thing IMO.

@bo-tato
Copy link

bo-tato commented Feb 13, 2024

It's worth noting hu.dwim.defclass-star which also offers shorter class definition macro is already included in CIEL as a dependency of fof, and nyxt browser is using https://github.com/atlas-engineer/nclasses which is their fork of hu.dwim.defclass-star. Just noting the different options, personally I haven't used CLOS much so I can't really comment on them. For advent of code and most scripts where I just need some basic data structure I'm just using defstruct, (slot-value struct 'slot-name) should work, though I almost always use with-slots. For redefinitions I just select the restart to redefine invalidating previous instances, which for single file advent of code problems or short scripts works fine for me though I imagine for developing larger programs interactively without losing state then redefining classes will work nicer than structs.

@vindarel
Copy link
Contributor Author

vindarel commented Feb 13, 2024

(slot-value struct 'slot-name) should work

oooh it does indeed, thanks. It's probably not portable, but it's good for AOC.

hu.dwim.defclass-star which also offers shorter class definition macro is already included in CIEL as a dependency of fof

good find. However the presence of FOF in CIEL remains uncertain (edit: fof was removed and replaced by file-finder, its little brother). Locally I discarded it, since it relies on Osicat, which complicates building and deploying CIEL. We can probably replace Osicat with another library, but that's not done yet (https://gitlab.com/ambrevar/fof/-/issues/6).

I also despise defclass-star for its lack of doc/readme/example and I discarded nclasses: it's over-engineered to my taste (and to CIEL's goals), and less concise than defclass/std.

ps: re osicat: Moira depends on it too, but that's been worked on last December (ruricolist/moira#1).

@bo-tato
Copy link

bo-tato commented Feb 13, 2024

Interesting info, and yea it seems you're right about slot-value, I found this discussion which says:

Nope, it's not. WITH-SLOTS uses SLOT-VALUE, which is unspecified for
objects with metaclass STRUCTURE-CLASS.

Though it seems it works fine on sbcl, ccl, lispworks. This comment from Rainer Joswig says probably every implementation besides GCL supports it. I just fired up GCL (2.6.12, the version in ubuntu repo which isn't the latest), and it's bizarre, slot-value doesn't work on structs, but because it returns undefined function for slot-value and also defclass, it seems GCL is just missing CLOS. This thread about defclass not existing in GCL says:

GCL is still a very old Common Lisp and does not purport to conform (as
the formal language goes) to the ANSI standard for Common Lisp. It is a
Common Lisp that refers to the 1984 book Common Lisp the Language.

@vindarel vindarel added the enhancement New feature or request label Sep 2, 2024
@vindarel
Copy link
Contributor Author

vindarel commented Sep 2, 2024

update: fof was removed and replaced by file-finder, its little brother, sans defclass-star.

https://github.com/lisp-maintainers/file-finder

@vindarel
Copy link
Contributor Author

vindarel commented Sep 9, 2024

I'll add a (print-object/std foo) macro to print the slots' value by default: lisp-maintainers/defclass-std#2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants