[racket] using a binding for-syntax in a module, and also providing it from that module

From: Brian Mastenbrook (brian at mastenbrook.net)
Date: Tue Mar 20 15:28:39 EDT 2012

On 03/20/2012 12:08 PM, Neil Van Dyke wrote:
> In the below example, can file "library.rkt" be changed (without
> splitting it up into two files), so that file "program.rkt" works
> without change? Is using "racket/load" the only way to keep
> "library.rkt" to one file like this?

Basically, you just need to make sure that my-syntax-util is defined in 
both phase 0 and phase 1. You could put it in a module that you require 
twice, but that's roughly equivalent to just defining it twice, once at 
phase 0 and once at phase 1 using `begin-for-syntax':

#lang racket/base

(require (for-syntax racket/base))

(define-syntax begin-both
   (syntax-rules ()
     ((_ e ...)
        (begin-for-syntax e ...)
        e ...))))

   (define (my-syntax-util stx)
     ;; Pretend that "my-syntax-util" performs some big complicated 
     ;; which is useful both in the implementation of "my-own-syntax" and
     ;; also directly by users of this library.
     (quasisyntax/loc stx
       (cons 'my-syntax-util-was-here #,stx))))

(define-syntax (my-own-syntax stx)
   (syntax-case stx ()
      (quasisyntax/loc stx
        (if CONDITION
            #,(my-syntax-util (syntax VAL))

(provide my-own-syntax

Brian Mastenbrook
brian at mastenbrook.net

Posted on the users mailing list.