Last active
June 18, 2023 21:02
-
-
Save kurinoku/1034f087b928ef9e0cfc3f0f5b4ad951 to your computer and use it in GitHub Desktop.
Use identifier-binding to check if an identifier is defined or not
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
#lang racket | |
(require (for-syntax racket/syntax | |
syntax/parse)) | |
(module+ test | |
(require rackunit) | |
(let () | |
(define/if-not-exists t 0) | |
(check-equal? t 0)) | |
(check-not-exn | |
(lambda () | |
(define/if-not-exists t #t) | |
(define/if-not-exists t '()) | |
(check-equal? t #t)))) | |
(define-syntax (define/if-not-exists stx) | |
(syntax-parse stx | |
[(_ x:id value:expr) | |
#:when (not (identifier-binding #'x)) | |
(syntax/loc stx (define x value))] | |
[(_ x:id value:expr) | |
(syntax/loc stx (void))])) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment