* boiler plate class generation, writing fresh variables with macros
@ 2022-07-22 9:43 Blake Shaw
2022-07-22 9:58 ` Maxime Devos
0 siblings, 1 reply; 2+ messages in thread
From: Blake Shaw @ 2022-07-22 9:43 UTC (permalink / raw)
To: guile-user
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
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: boiler plate class generation, writing fresh variables with macros
2022-07-22 9:43 boiler plate class generation, writing fresh variables with macros Blake Shaw
@ 2022-07-22 9:58 ` Maxime Devos
0 siblings, 0 replies; 2+ messages in thread
From: Maxime Devos @ 2022-07-22 9:58 UTC (permalink / raw)
To: Blake Shaw, guile-user
[-- Attachment #1.1.1: Type: text/plain, Size: 2272 bytes --]
On 22-07-2022 11:43, Blake Shaw wrote:
> 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"))
> 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.
Syntax transformations in Scheme work from the outside to the inside,
not the other way around, so you can't do things like this (define-class
doesn't know what to do with this 'slot-machine' thing, it will reject
it for not being a valid slot definition). However, you can define a
syntax that generates the surrounding define-class and interprets things
to insert the result of slot-matchine into a proper define-class form.
Something like (untested):
(define-syntax easy-define-class
(lambda (s)
(syntax-case s ()
((_ <class-name> <parents> slot ...)
#`(define-class <class-name> <parents> #,(fixup #'slot) ...)))))
(define slot-machine "todo: use define-syntax + identifier-syntax +
syntax-error etc.")
(define (fixup slot)
(syntax-case slot (slot-machine)
((slot-machine category quality value)
(let ((sym ...)
...)
#`(#:getter #,get-sym [etcetera])))
(boring #'boring))) ; old-style slot definitions
(easy-define-class <dummy> () (slot-machine ...))
Important: fixup is _not_ a macro, just a helper procedure of the
easy-define-class macro that happens to transform syntax! As such, it
needs to use 'define', not 'define-syntax'. Also, you probably can't do
symbol-append like you are doing currently as Guile's hygiene code won't
know the context of the generated symbol, so you'll need some
datum->syntax + syntax->datum + symbol-append for your technically
unhygienic 'fixup' (though a very mild form of inhygiene!).
Greetings,
Maxime.
[-- Attachment #1.1.2: OpenPGP public key --]
[-- Type: application/pgp-keys, Size: 929 bytes --]
[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 236 bytes --]
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2022-07-22 9:58 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-22 9:43 boiler plate class generation, writing fresh variables with macros Blake Shaw
2022-07-22 9:58 ` Maxime Devos
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).