Skip to content

Instantly share code, notes, and snippets.

@benknoble
Last active June 22, 2021 21:23
Show Gist options
  • Save benknoble/53a32f2f7280f7876bd7064649f7d215 to your computer and use it in GitHub Desktop.
Save benknoble/53a32f2f7280f7876bd7064649f7d215 to your computer and use it in GitHub Desktop.
using info.rkt for configuration in Racket
#lang racket
(provide (all-defined-out))
(require setup/getinfo)
(define config?-v1 (get-info/full "."))
(config?-v1 'name) ;=> "Games"
(config?-v1 'gracket-launcher-libraries) ;=> '("main.rkt")
(config?-v1 'gracket-launcher-names) ;=> '("PLT Games")
;; (config?-v1 'not-present) ;=> info.rkt: no info for dne [,bt for context]
(define config?-v2
(let ([get-info (get-info/full ".")])
(λ (key)
(get-info key (const #f)))))
(config?-v2 'name) ;=> "Games"
(config?-v2 'gracket-launcher-libraries) ;=> '("main.rkt")
(config?-v2 'gracket-launcher-names) ;=> '("PLT Games")
(config?-v2 'not-present) ;=> #f
(define config?-v3
(let* ([get-info (get-info/full ".")]
[default-config #hash([other . "hello"])]
[from-default-config-thunk
(λ (key)
(thunk (hash-ref default-config key #f)))])
(λ (key)
(get-info key (from-default-config-thunk key)))))
(config?-v3 'name) ;=> "Games"
(config?-v3 'gracket-launcher-libraries) ;=> '("main.rkt")
(config?-v3 'gracket-launcher-names) ;=> '("PLT Games")
(config?-v3 'not-present) ;=> #f
(config?-v3 'other) ;=> "hello"
(require syntax/parse/define)
(define-syntax-parse-rule (define-config name:id path:expr default:expr)
(define name
(let* ([get-info (get-info/full path)]
[default-config default]
[from-default-config-thunk
(λ (key)
(thunk (hash-ref default-config key #f)))])
(λ (key)
(get-info key (from-default-config-thunk key))))))
(define-config config?-v4 "." #hash([other . "hello"]))
(config?-v4 'name) ;=> "Games"
(config?-v4 'gracket-launcher-libraries) ;=> '("main.rkt")
(config?-v4 'gracket-launcher-names) ;=> '("PLT Games")
(config?-v4 'not-present) ;=> #f
(config?-v4 'other) ;=> "hello"
#lang info
(define name "Games")
(define gracket-launcher-libraries '("main.rkt"))
(define gracket-launcher-names '("PLT Games"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment