-
-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change dynamic-require wrappers; DRY doc index tests
- Loading branch information
1 parent
b563f9e
commit f1f04fa
Showing
7 changed files
with
109 additions
and
98 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
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,38 @@ | ||
;; Copyright (c) 2024 by Greg Hendershott. | ||
;; SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
#lang racket/base | ||
|
||
(require (for-syntax racket/base | ||
"safe-dynamic-require.rkt") | ||
syntax/parse/define) | ||
|
||
(provide define-fallbacks) | ||
|
||
;; safe-dynamic-require is most useful in scenarios where an entire | ||
;; module might not be installed. Note that tools like | ||
;; go-to-definition will always go to the safe-dynamic-require site, | ||
;; because that is the binding site. Any binding from a normal | ||
;; (non-dynamic) require is shadowed by the dynamic require. | ||
;; | ||
;; Another scenario is where a module is always installed, but over | ||
;; time has added exports; therefore an older version might be | ||
;; installed. In this case it can be nicer to do a plain, non-dynamic | ||
;; require of the module, and use define-fallbacks to create | ||
;; definitions /only/ for identifiers not supplied by the installed | ||
;; version of the module. As a result, tools like go-to-definition | ||
;; will handle normally imported bindings in the usual way (go to the | ||
;; definition in that other module's source), which is very | ||
;; convenient. | ||
|
||
(define-syntax-parser define-fallback | ||
[(_ mod:id (id:id arg:expr ...) body:expr ...+) | ||
(if (safe-dynamic-require (syntax-e #'mod) (syntax-e #'id)) | ||
#'(begin) | ||
#'(define (id arg ...) | ||
body ...))]) | ||
|
||
(define-syntax-parser define-fallbacks | ||
[(_ mod:id [(id:id arg:expr ...) body:expr ...+] ...+) | ||
#`(begin | ||
(define-fallback mod (id arg ...) body ...) ...)]) |
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
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
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,33 @@ | ||
;; Copyright (c) 2024 by Greg Hendershott. | ||
;; SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
#lang racket/base | ||
|
||
(require (for-syntax racket/base) | ||
syntax/parse/define) | ||
|
||
(provide safe-dynamic-require | ||
module-installed? | ||
rhombus-installed?) | ||
|
||
;; Although dynamic-require calls `fail-thunk` when `id` does not | ||
;; exist in `mod`, it raises exn:fail if `mod` doesn't exist. | ||
;; | ||
;; This wrapper calls fail-thunk called consistently. | ||
;; | ||
;; We define this in a submodule in order to require it at both phase | ||
;; 0 and 1, the latter for use in the define-fallbacks macro below. | ||
(define (safe-dynamic-require mod id [fail-thunk (λ () #f)]) | ||
(with-handlers ([exn:fail? (λ _ (fail-thunk))]) | ||
(dynamic-require mod id fail-thunk))) | ||
|
||
;; Some predicates useful for e.g. tests that may run against various | ||
;; versions of Racket. | ||
|
||
(define (module-installed? mod) | ||
(and (safe-dynamic-require mod #f) | ||
#t)) | ||
|
||
(define rhombus-installed? | ||
(let ([v (module-installed? 'rhombus)]) | ||
(λ () v))) |
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
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