-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#lang racket/base | ||
(module+ test | ||
(require rackunit syntax-parse-example/conditional-require/conditional-require | ||
syntax/macro-testing) | ||
|
||
(conditional-require #true | ||
racket/list | ||
racket/match) | ||
|
||
(check-pred values partition) | ||
|
||
(conditional-require #false | ||
racket/math | ||
racket/flonum) | ||
|
||
(check-pred values flabs) | ||
|
||
(check-exn exn:fail:syntax? | ||
(λ () | ||
(convert-compile-time-error | ||
(conditional-require 1 racket/unsafe/ops typed/racket)))) | ||
|
||
(check-exn exn:fail:syntax? | ||
(λ () | ||
(convert-compile-time-error | ||
(conditional-require #false 1 2)))) | ||
|
||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#lang racket/base | ||
(provide conditional-require) | ||
(require (for-syntax racket/base syntax/parse)) | ||
|
||
(begin-for-syntax | ||
(define-syntax-class mod-name | ||
(pattern _:id) | ||
(pattern _:string))) | ||
|
||
(define-syntax (conditional-require stx) | ||
(syntax-parse stx | ||
[(_ test:boolean r1:mod-name r2:mod-name) | ||
(if (syntax-e #'test) | ||
#'(require r1) | ||
#'(require r2))])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#lang syntax-parse-example | ||
@require[ | ||
(for-label racket/base syntax/parse syntax-parse-example/conditional-require/conditional-require)] | ||
|
||
@(define conditional-require-eval | ||
(make-base-eval '(require syntax-parse-example/conditional-require/conditional-require))) | ||
|
||
@title{@tt{conditional-require}} | ||
|
||
@; ============================================================================= | ||
|
||
This macro conditionally requires one of two module paths based on a compile-time condition. | ||
|
||
@racketfile{conditional-require.rkt} | ||
|
||
Notes: | ||
@itemlist[ | ||
@item{ | ||
The syntax class @racket[mod-name] matches syntactic strings or identifiers. | ||
This doesn't guarantee that the second and third argument to @racket[conditional-require] | ||
are valid module paths, but it rules out nonsense like @racket[(conditional-require #true (+ 2 2) 91)]. | ||
} | ||
@item{ | ||
The test could be more interesting. | ||
It could branch on the value of @racket[current-command-line-arguments], | ||
or do a @racket[case] based on @racket[system-type]. | ||
} | ||
] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters