Hi all,

I'm programming in Racket for many years and still haven't used macros
or delved into how they work. Kind of embarrassing... anyway, it should
be clear what I want to achieve in the code below. I like to wrap a
module around framework/preferences, abstracting from the preference
implementation while at the same time exporting symbols of the form
pref:identifier - I want unknown preferences to be detected at compile
time.

The pref helper macro works. But I'm doing something obviously wrong in
defpref, since it gives an error "db-path: unbound identifier in
module". How do I *define* the derived identifier, as in name ->
pref:name, with a syntax macro? Does "definition-key" have to be a
macro as well?

Best,

Erich

-------
#lang racket
(require framework/preferences)
(provide init-preferences const:app-name const:app-version
         pref:db-path)

(define const:app-name "Virtual Assistant")
(define const:app-version 1.0)

(define default-datapath (build-path (find-system-path 'home-dir)
"Documents" "va"))

(define *defaults* '())

(define (lookup-key ident)
  (string->symbol (string-append "va:" (symbol->string ident))))

(define (definition-key ident)
  (string->symbol (string-append "pref:" (symbol->string ident))))

(define-syntax pref
  (syntax-rules ()
    [(pref ident) (let ((key (lookup-key ident)))
                    (unless (preferences:default-set? key)
(init-preferences)) (preferences:get key))]
    [(pref ident value) (let ((key (lookup-key ident)))
                    (unless (preferences:default-set? key)
(init-preferences)) (preferences:set key value))]
    [(pref ident expr test?)
     (set! *defaults* (cons (list (lookup-key ident) expr test?)
*defaults*))]))

(define-syntax defpref
  (syntax-rules ()
    [(defpref ident default test?) (begin
                                     (define (definition-key ident)
                                       (case-lambda
                                         [() (pref ident)]
                                         [(value) (pref ident value)]))
                                     (pref ident default test?))]))
                                    

(defpref db-path default-datapath path?)

(define (init-preferences)
  (make-directory* default-datapath)
  (for-each
   (lambda (triple)
     (preferences:set-default (first triple) (second triple) (third
triple))) *defaults*))

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to