At Wed, 29 Jul 2015 07:00:54 -0700 (PDT), Klaus Ostermann wrote:
> Units are not allowed to export macros, presumably because the unit wiring 
> takes place after the macro expansion.
> 
> I have a unit and would like to define a few macros based on the interface of 
> that unit.
> 
> Right now I copy&paste my macros into every file where I wire the unit, such 
> as in
> 
> ...
> (define-values/invoke-unit/infer some-unit@)
> 
> (define-syntax-rule (foo t) (...something-from-the-unit-interface))
> 
> Surely there is a better way to do this.
> 
> But how?

You can include macros in a unit signature. For example,

 (define-signature s^
   (;; A plain export:
    call-with-catch

    ;; Macro that uses the export:
    (define-syntaxes (with-catch)
      (syntax-rules ()
        [(_ body0 body ...)
         (call-with-catch
          (lambda () body0 body ...))]))))

defines the signature of a unit that provides `call-with-catch`, but
also arranges that any importing context can use `with-catch`.

Given

 (define-unit u@
   (import)
   (export s^)
   (define (call-with-catch thunk)
     (with-handlers ([exn:fail? (lambda (exn) exn)])
       (thunk))))

then this works:

 (define-values/invoke-unit/infer u@)

 (with-catch (/ 0))


The `define-signature` form currently only recognizes `define-syntaxes`
as the form for adding macros to a signature, which is why I used that
form plus `syntax-rules` instead of `define-syntax-rules`.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to