<div dir="ltr"><div>This worked — for some reason, moving the '(file ...)' expression into the `with-syntax` expression made a difference.</div><div><br></div><div>(define-syntax (overriding-require+provide-with-prefix stx)</div><div> (syntax-case stx () [(_ main override out-prefix)</div><div> (let ([path-to-override (path->string (build-path (current-directory) (syntax->datum #'override)))])</div><div> (if (file-exists? path-to-override)</div><div> (with-syntax ([override (datum->syntax stx `(file ,(datum->syntax stx path-to-override)))])</div><div> #'(begin </div><div> (require (combine-in override (subtract-in main override)))</div><div> (provide (prefix-out out-prefix (combine-out (all-from-out main) (all-from-out override))))))</div><div> #'(begin </div><div> (require main)</div><div> (provide (prefix-out out-prefix (all-from-out main))))))]))</div><div><br></div><div><br></div><div>(overriding-require+provide-with-prefix "main.rkt" "override.rkt" the-prefix:)</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Oct 24, 2014 at 1:20 PM, Matthew Butterick <span dir="ltr"><<a href="mailto:mb@mbtype.com" target="_blank">mb@mbtype.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">I know that a hard-coded absolute path can be used in `require`:<br>
<br>
(require (file "/path/to/directory/module.rkt"))))<br>
<br>
But how can a generated path be used? Like so:<br>
<br>
(require (file (path->string (build-path (current-directory) "module.rkt"))))<br>
<br>
In particular, I want to `require` the file if it exists, or otherwise skip it.<br>
<br>
I thought it should be a macro like this, but it doesn't work. The correct `require` syntax pops out, but it doesn't bind any identifiers.<br>
<br>
(define-syntax (try-current-directory-require stx)<br>
(syntax-case stx ()<br>
[(_ filename)<br>
(with-syntax ([path-string (datum->syntax stx (path->string (build-path (current-directory) (syntax->datum #'filename))))])<br>
(if (file-exists? (syntax->datum #'path-string))<br>
#'(require (prefix-in local: (file path-string)))<br>
#'(void)))]))<br>
<br>
(try-current-directory-require "module.rkt")<br>
<br>
<br>
Any suggestions? Is this a job for `define-require-syntax`?<br>
<br>
</blockquote></div><br></div>