Hello, I’ve been trying to write a macro which calls define appending a prefix to an id
. Roughly speaking I was thinking I could create a syntax object which looked like #
(define #,(local-expand #’(append-prefix id)) definition )but the identifier isn't found. I experimented a bit further and I found that calling what I'd assumed to be
identity = (datum->syntax #f (syntax->datum #’id))` didn’t work either.
I’m clearly missing something about manipulating syntax objects here - what should I be looking at?
- Use
format-id
to create the new identifier. - Use
with-syntax
to turn your new identifier into a pattern variable. - Use
syntax
orsyntax/loc
to generate the piece of code.
#lang racket
(require (for-syntax racket/syntax syntax/parse))
(define-syntax (define-foo stx)
(syntax-parse stx
[(_define-foo id)
; 1. Generate the new identifier
(define foo-id (format-id #'id "foo-~a" #'id))
; 2. Turn it into a pattern variable
(with-syntax ([foo-id foo-id])
; 3. Use it in a template
(syntax/loc stx
(define foo-id 42)))]))
; Example
(define-foo bar) ; define foo-bar
foo-bar ; reference foo-bar
Ah OK - so it was the ctx
which I was losing when doing the transforms?
Yes.
The pdf on https://download.racket-lang.org/releases/6.10.1/pdf-doc/foreign.pdf\|ffi/unsafe has a rather cryptic sentence if a resource is scarce or visible to end users, then custodian management is more appropriatethan mere finalization as implemented by allocator.
. Why is this the case? Is a custodian not simply a tree like model of resource frees? Or am I missing something?
pg. 11 for reference.
Thank you!
That’s because finalization may happen after a long time, and is hard for users to speed up
Whereas custodian management is tree shaped and deterministic
Ah I see; because it’s tied to the GC?
Yes
Thank you, that makes sense.
@zainab.ali.london has joined the channel