Conditional define-syntax

I'm trying to not load an FFI and define all functions when it's not available, defining the functions to be something like 'error' on call.

Is it possible to do a define-syntax based on the value of a variable? e.g., something like this:

(define-ffi-definer def-rktwebview webview-lib)

(define-syntax def-rktwebiew-dummy
  (syntax-rules ()
    ((_ f def)
     (λ args (error "Not implemented")))))

(define define-rktwebview #f)

(if (eq? do-ffi #t)
    (set! define-rktwebview def-rktwebview)
    (set! define-rktwebview def-rktwebview-dummy))

You can use #:default-make-fail make-not-available in define-ffi-definer and it will do what I think you want.

1 Like

Thanks, I just created this (read the manual of ffi-definer).

(define (make-ffi-repl id err . ret)
  (let ((warned #f)
        (msg (if (eq? do-ffi #t)
                 (string-append
                  "'~a' could not be loaded from "
                  (format "~a" webview-lib-file))
                 (string-append
                  "'~a' could not be loaded.\n"
                  reason)))
        )
    (λ () (λ args
            (if err
                (error (format msg id))
                (begin
                  (unless warned
                    (displayln (format msg id))
                    (set! warned #t))
                  (car ret)))))))

(define-ffi-definer define-rktwebview
                    webview-lib
                    #:default-make-fail (λ (id)
                                          (if (eq? do-ffi #f)
                                              (cond
                                                ((eq? id 'rkt_webview_env)
                                                 (make-ffi-repl id #f #t))
                                                ((eq? id 'rkt_webview_events_waiting)
                                                 (make-ffi-repl id #f 0))
                                                ((eq? id 'rkt_webview_init)
                                                 (make-ffi-repl id #f #t))
                                                ((eq? id 'rkt_webview_cleanup)
                                                 (make-ffi-repl id #f #t))
                                                (else
                                                 (make-ffi-repl id #t))
                                                )
                                              (make-ffi-repl id #t)))
  )