Opened 3 years ago

Last modified 3 years ago

#1780 new defect

wishlist: support keywords in coops (make <class> ...) form

Reported by: Shawn Rutledge Owned by:
Priority: major Milestone: someday
Component: extensions Version: 5.2.0
Keywords: Cc:
Estimated difficulty:

Description

As described in SRFI-88, keywords are nice to have for widget constructors:

(tcltk-frame relief: 'ridge

borderwidth: 4
height: "50px"
width: "100px")

but if widgets are coops classes, it's natural to use the make form rather than a separate constructor procedure for each class:

(make <widget> width: 100 height: 50 ...)

I got it working with a quick hack (which no doubt could be more elegant):

Index: coops.scm
===================================================================
--- coops.scm   (revision 40393)
+++ coops.scm   (working copy)
@@ -9,9 +9,9 @@
 
 (declare (disable-interrupts))
 
-(import scheme matchable srfi-1 record-variants
+(import scheme matchable srfi-1 record-variants symbol-utils
         (chicken condition) (chicken fixnum) (chicken format)
-        (chicken syntax) (chicken sort)
+        (chicken keyword) (chicken syntax) (chicken sort)
         (only miscmacros ensure))
 (import-for-syntax matchable (chicken plist) srfi-1)
 
@@ -176,6 +176,7 @@
           (sv (make-vector (length slotnames) uninitialized))
           (i (make-coops-instance c sv)))
       (define (slot-index name)
+        (when (keyword? name) (set! name (keyword->symbol name)))
        (or (position name slotnames)
            (error "no such slot in instances of given class" name c)))
       (let loop ((svsv svsv))

Then one might expect it to be possible for slot names to be keywords in every other use case too (such as slot accessors); but I have custom syntax implemented for those anyway, so far like this:

(-> widget width) ; no colon, because it would look funny; no quote, for convenience
(=> widget width: 50)

I'd rather not have to wrap make in something custom, because it's already such a nice name.

In Chicken 4 it wasn't an issue, because (symbol? foo:) is true.

Change History (2)

comment:1 Changed 3 years ago by Shawn Rutledge

Another chicken 4 trick was to define public slots as keywords and private ones as symbols.

(define-class <rect> () ((x: 0) (y: 0) (w: 10) (h: 10) (private 'foo)))
(define r (make <rect> x: 50 'private "whee"))
(slot-ref r x:)
(slot-ref r 'private)

then I could make private slots inaccessible and convert public slot names to keywords in the (-> ...) syntax:

(-> r x) ; no colon needed
(-> r private) ; not allowed

(raspbian still has chicken 4, so I was able to reproduce this on a raspberry pi)

Version 0, edited 3 years ago by Shawn Rutledge (next)

comment:2 Changed 3 years ago by Shawn Rutledge

So maybe another way to fix this would be to add a parameter or something that I can set in my eggs to make keywords be symbols again, if doing that would not break other code. (or maybe there already is such a thing?)

Note: See TracTickets for help on using tickets.