[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
boiler plate class generation, writing fresh variables with macros
From: |
Blake Shaw |
Subject: |
boiler plate class generation, writing fresh variables with macros |
Date: |
Fri, 22 Jul 2022 09:43:20 +0000 |
Hiya guilers,
Right now I am working on a window manager extension system in Guile and
GOOPs, and I want to eliminate the boilerplate for generating class slots,
with a syntax-case macro like:
#+begin_example scheme
(define-syntax slot-machine
(λ (form)
(syntax-case form ()
((_ category quality value)
#'(let* ((sym (symbol-append category '- quality))
(set-sym! (symbol-append 'set- sym '!))
(get-sym (symbol-append 'get- sym))
(acc-sym (symbol-append 'acc- sym)))
(quasiquote (,sym #:init-value value
#:setter ,set-sym!
#:getter ,get-sym
#:accessor ,acc-sym))))))
#+end_example
With this I can evaluate (slot-machine 'inner 'color "green") resulting in
what looks like an acceptable slot definition:
=> (inner-color #:init-value "#BF3D52" #:setter set-inner-color! #:getter
get-inner-color #:accessor acc-inner-color)
Indeed, if I define a class with this slot definition in place (ie
manually/without calling the macro), it works fine:
#+begin_example scheme
(define-class <dummy> ()
(inner-color #:init-value "#BF3D52" #:setter set-inner-color! #:getter
get-inner-color #:accessor acc-inner-color))
(describe <dummy>)
=> <dummy> is a class. It's an instance of <class>
Superclasses are:
<object>
Directs slots are:
inner-color
(No direct subclass)
Class Precedence List is:
<dummy>
<object>
<top>
Class direct methods are:
Method #<<accessor-method> (<dummy> <top>) 7f7b27e10ac0>
Generic: setter:acc-inner-color
Specializers: <dummy> <top>
Method #<<accessor-method> (<dummy>) 7f7b27e10b00>
Generic: acc-inner-color
Specializers: <dummy>
Method #<<accessor-method> (<dummy> <top>) 7f7b27e10b40>
Generic: set-inner-color!
Specializers: <dummy> <top>
Method #<<accessor-method> (<dummy>) 7f7b27e10b80>
Generic: get-inner-color
Specializers: <dummy>
#+end_example
But if I try to use `slot-machine` inside a class definition i'm out of
luck:
(define-class <dummy> ()
(slot-machine 'inner 'color "green"))
=> While compiling expression:
Syntax error:
socket:7257:0: source expression failed to match any pattern in form
(define-class-pre-definition ((quote inner) (quote color) "green"))
I have tried to remedy this ina number of ways, using datum->syntax,
quasisyntax/unsyntax, make-variable and by defining a new syntax-case macro
to define classes, all without luck.
This is actually a recurring theme with my experience with Guile, working
on a project, needing to generate boilerplate, and then being unable to
find a result, so I figured its time I reach out to figure out what I'm
doing wrong in this situation.
Thanks for your help!
Best,
Blake
- boiler plate class generation, writing fresh variables with macros,
Blake Shaw <=