From: Maxime Devos <maximedevos@telenet.be>
To: Blake Shaw <blake@sweatshoppe.org>, guile-user <guile-user@gnu.org>
Subject: Re: boiler plate class generation, writing fresh variables with macros
Date: Fri, 22 Jul 2022 11:58:00 +0200 [thread overview]
Message-ID: <ef6eed1a-104d-0b3a-9552-7613931594ab@telenet.be> (raw)
In-Reply-To: <CAKjmbcBDKkL60Wfi4XP4SpD9a0fpBmj3p62hwkh8k9z8hYMHOw@mail.gmail.com>
[-- 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 --]
prev parent reply other threads:[~2022-07-22 9:58 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-07-22 9:43 boiler plate class generation, writing fresh variables with macros Blake Shaw
2022-07-22 9:58 ` Maxime Devos [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/guile/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=ef6eed1a-104d-0b3a-9552-7613931594ab@telenet.be \
--to=maximedevos@telenet.be \
--cc=blake@sweatshoppe.org \
--cc=guile-user@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).