Opened 3 months ago

Last modified 3 months 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 months 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)

Maybe you have valid arguments for why private slots are a bad idea though. Data hiding is a classic-OO trope, but perhaps not such a good fit in functional style. My first reaction to C++ when I learned it about 27 years ago was I thought that it's rather paranoid if you distrust your fellow programmers so much that you need to hide data from them. OTOH there's something to the idea of decluttering the public API. So I was using the keyword/symbol distinction tentatively as a workaround to hide a few things. Apparently I have to stop doing that now.

Last edited 3 months ago by Shawn Rutledge (previous) (diff)

comment:2 Changed 3 months 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.