Let's take a look at this error:

    "format-id: undefined;
     cannot reference an identifier before its definition
      phase: 1"

Since format-id is undefined, we need to require the corresponding module.
The docs say the identifier is from racket/syntax.

Now

    (require racket/syntax)   will import racket/syntax to phase 0 (runtime)

and

    (require (for-syntax racket/syntax))   will import racket/syntax to
phase 1 (compile time).

In general: the body of a define-syntax runs on compile time, so all
identifiers needs to required using (for-syntax ...).
If in doubt:  (require racket/syntax (for-syntax racket/syntax)).

Note: If you want to play with, say format-id in the repl, note that
the repl is in phase 0 - which means (require racket/syntax) is
needed before format-id can be used in the repl.

/Jens Axel






2017-01-12 18:37 GMT+01:00 Erich Rast <er...@snafu.de>:

> Thanks for your your help. Unfortunately, I haven't managed to get my
> head around the phases and the question of when symbols are quoted or
> evaluated. I tried Matthew Buttericks change but it yields an error:
>
> "format-id: undefined;
>  cannot reference an identifier before its definition
>   phase: 1"
>
> So I tried to implement Matthias Felleisen's suggestion (see below), but
> now I get "home-dir: unbound identifier in module in: home-dir". Why
> does this occur and how can I fix this? And is there a way to avoid
> quoting db-path when using defpref?
>
> Best,
>
> Erich
>
> --------
> #lang racket
> (require framework/preferences)
>          ;"constants.rkt")
> (provide (prefix-out pref: init) (prefix-out pref: db-path))
>
> (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-syntax pref
>   (syntax-rules ()
>     [(pref ident) (let ((key (lookup-key ident)))
>                     (unless (preferences:default-set? key) (init))
>                     (preferences:get key))]
>     [(pref ident value) (let ((key (lookup-key ident)))
>                     (unless (preferences:default-set? key) (init))
>                     (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 ident
>                                        (case-lambda
>                                          [() (pref ident)]
>                                          [(value) (pref ident value)]))
>                                      (pref ident default test?))]))
>
> (defpref 'db-path default-datapath path?)
>
> (define (init)
>   (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.
>



-- 
-- 
Jens Axel Søgaard

-- 
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