Skip to content

Using keyword symbols as slot-descriptors in DEFSTRUCT #58

@arbv

Description

@arbv

From https://groups.google.com/d/msg/cormanlisp/_qB2KC4Nw5Q/Sgv6xoZ0CQAJ

I suspect using keyword symbols as slot-descriptors in defstruct doesn't meet HyperSpec and outcome is undefined.
I was trying it as I have a list of keyword symbols I was thinking of using to create a structure with them as slot names.

Corman Lisp handles this except for initializing the the slots when making instances. Setf to slot later works.

e.g.
;; Corman Lisp 3.1 (Patch level 2)
(defstruct book :title :pages)
BOOK
(setq book1 (make-book :title "hoho" :pages 190))
#S( BOOK :TITLE :TITLE :PAGES :PAGES )
book1
#S( BOOK :TITLE :TITLE :PAGES :PAGES )

(book-p book1)
T
(book-title book1)
:TITLE
(setf (book-title book1) "hihi")
"hihi"
(book-title book1)
"hihi"

SBCL initializes as might be expected or hoped for, but with warnings

This is SBCL 1.4.14, an implementation of ANSI Common Lisp.

  • (defstruct book :title :pages)
    STYLE-WARNING: slot name of :TITLE indicates possible syntax error in DEFSTRUCT
    STYLE-WARNING: slot name of :PAGES indicates possible syntax error in DEFSTRUCT
    BOOK

  • (setq book1 (make-book :title "hi" :pages 190))
    ; in: SETQ BOOK1
    ; (SETQ BOOK1 (MAKE-BOOK :TITLE "hi" :PAGES 190))
    ;
    ; caught WARNING:
    ; undefined variable: BOOK1
    ;
    ; compilation unit finished
    ; Undefined variable:
    ; BOOK1
    ; caught 1 WARNING condition
    #S(BOOK :TITLE "hi" :PAGES 190)

  • book1
    #S(BOOK :TITLE "hi" :PAGES 190)

  • (book-p book1)
    T

  • (book-title book1)
    "hi"

  • (setf (book-title book1) "hihi")
    ; in: SETF (BOOK-TITLE BOOK1)
    ; (THE BOOK BOOK1)
    ;
    ; caught WARNING:
    ; undefined variable: BOOK1
    ;
    ; compilation unit finished
    ; Undefined variable:
    ; BOOK1
    ; caught 1 WARNING condition
    "hihi"

  • book1
    #S(BOOK :TITLE "hihi" :PAGES 190)

What do you think of Corman Lisp behavior?

Cheers
Nigel

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions