File: safe-dynamic-require.rkt

package info (click to toggle)
racket-mode 20250711~git.8a80578-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,024 kB
  • sloc: lisp: 17,215; makefile: 106
file content (26 lines) | stat: -rw-r--r-- 766 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
;; Copyright (c) 2024 by Greg Hendershott.
;; SPDX-License-Identifier: GPL-3.0-or-later

#lang racket/base

(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 consistently.
(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?)
  (module-installed? 'rhombus))