Initialization in defstruct using keyword symbols

46 views
Skip to first unread message

Nigel Brown

unread,
Mar 15, 2019, 6:47:46 PM3/15/19
to cormanlisp
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

Nigel Brown

unread,
Mar 15, 2019, 6:51:18 PM3/15/19
to cormanlisp
PS GCL doesn't like it:
GCL (GNU Common Lisp)  Version(2.5.0) Tue Jan 14 14:23:46 EAST 2003

>(defstruct book :title :pages)

BOOK

>(setq book1 (make-book :title "hi" :pages 190))

Error: :TITLE is not a variable.
Fast links are on: do (si::use-fast-links nil) for debugging
Error signalled by MAKE-BOOK.
Broken at MAKE-BOOK.  Type :H for Help.

Artem Boldariev

unread,
Mar 20, 2019, 6:27:49 AM3/20/19
to cormanlisp
It seems like a bug for me, but it is rather minor as it will not affect the standard compliant code (as far as I can tell). Thank you for reporting it!

I have opened a new issue on Github.

суббота, 16 марта 2019 г., 0:47:46 UTC+2 пользователь Nigel Brown написал:
Reply all
Reply to author
Forward
0 new messages