[racket] Weird Require Syntaxes

From: YC (yinso.chen at gmail.com)
Date: Sat Sep 11 02:36:43 EDT 2010

Hopefully I read your message correctly that you are trying to define custom
'require' extensions, which the following could be what you are looking
for:

http://docs.racket-lang.org/reference/define.html#(mod-path._racket/require-syntax)

Caveat - I've never used require-syntax so don't know for certain if that's
what you need.

But you can also define your own "require" if you don't mind to use another
name, such as require/env, and then you can export it as require so other
modules can use it to replace the regular 'require' (of course if you want
to use other regular require specs you must take the care to reexpose them
with this approach.

Below is a sketch of what require/env looks like (you can replace #lang
scheme with #lang racket) - it only handle a single require spec as
written.

#lang scheme
(require (for-syntax scheme))

(begin-for-syntax
  ;; the environment database - extend through set-env!
  (define env-db
    (make-hash  `((url . net/url)
                  (cookie . net/cookie))))

  ;; return the module name based on the key
  (define (get-env key)
    (datum->syntax
     key
     (hash-ref env-db (syntax->datum key)
               (lambda ()
                 (error 'require/env "module ~a not exist" (syntax->datum
key))
                 ))))

  ;; sets a key for a particular module - even possible for require-specific
specs
  ;; such as prefix-in or only-in
  (define (set-env! key mod)
    (hash-set! env-db
               (syntax->datum key)
               (syntax->datum mod))
    #'(void))
  )

;; sets the alias for the require spec.
(define-syntax (define-require-env-alias! stx)
  (syntax-case stx ()
    ((~ key mod)
     (with-syntax ((result (set-env! #'key #'mod)))
       #'result))
    ))

;; require/env - use this to access the spec by alias.
(define-syntax (require/env stx)
  (syntax-case stx ()
    ((~ key)
     (with-syntax ((key (get-env #'key)))
       #'(require key)))
    ))

;; example usage -
(require/env url) ;; (require net/url)
(require/env cookie) ;; (require net/cookie)

;; maps date to (planet bzlib/date/srfi)
(define-require-env-alias! date (planet bzlib/date/srfi))

(require/env date)

(define-require-env-alias! xexpr-only (only-in xml xexpr? xexpr->string))

(require/env xexpr-only)


Hope this is what you are looking for.

Cheers,
yc


On Fri, Sep 10, 2010 at 10:51 PM, synx <plt at synx.us.to> wrote:

>
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> In my efforts to create a dynamically loading environment, I'd like to
> do something like this:
> - ---
> #lang racket/base
>
> (define-for-syntax db (something))
> (define-for-syntax (get-env key) (db-value db key))
>
> (define-syntax env
>  (lambda (form)
>    (syntax-case (form) ()
>      (#'(_ key)
>       (let ((key (syntax->datum (cadr form))))
>         (do-something-to
>          key
>          (datum->syntax
>           form
>           (get-env key)
>           form)))))))
>
> (require (env some-key))
> - ---
>
> But for the life of me I can't figure out how to do it. I looked into
> the import and import-source structures, but neither of them specify
> anything in regards to the value of what's being required. They just
> name it and the value is somehow acquired by other means. I tried
> putting something random in for the src-mod-path like 'env/foo but then
> racket actually tries to find and read the "env/foo.rkt" file in the
> current collections path, and won't just call get-env like I was hoping
> for.
>
> Basically what I'm looking for is some way to have the modules required
> be an evaluable module form returned by just some procedure, instead of
> being filesystem files or planet packages. The procedure would get the
> module from a database, and possibly as a side effect also trigger the
> recalculation of any other evaluation contexts that require that
> particular module.
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v2.0.14 (GNU/Linux)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
>
> iQEcBAEBAgAGBQJMixjiAAoJECC/cKf8E7UIXAUH/2Zf3NsJ+jiJ47F3LRHh28Y2
> 8wNMbI3W/JhPlQLBSDga5p3qEzrLX17cHFm3Tx8eE4vMLPVd7+gz2QKTWvNPFz6Q
> 37chCXsHfUnnCP3rOMBvqoJKz2mlvfqjLj4F+/GDxwR+Ua3UAsocKvvk7TlT8yM7
> /lmVKIP1WcMLXLMLst+HuUYjFYMOzOtmog1x4/nA66cYJiDD1IgX1gX9h1HxqyDP
> HNFYih43fE4IZUXERBfFDW9C48Wj3cgcauiWfGmh+2GdrDvnJP+SzRSCnInJSpTT
> Fp/V3MCceoN9ue9RNEktze/ym4ixnXUra+nBOHCgwImh8BSSWbu3AZIGGNLDWAQ=
> =SsEf
> -----END PGP SIGNATURE-----
> _________________________________________________
>  For list-related administrative tasks:
>  http://lists.racket-lang.org/listinfo/users
>



-- 
Cheers,
yc

Taming the software dragon - http://dragonmaestro.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20100910/914807d1/attachment.html>

Posted on the users mailing list.