Just for fun, if you want to be a little bit naughty, you can define
a custom #%top transformer that will make unbound identifiers that
are fully qualified automatically resolve to their imported values.
Just do a little bit of processing on the unbound identifiers:
#lang racket
(require (for-syntax racket/list
racket/match
racket/string
syntax/parse))
(define-syntax #%top
(syntax-parser
[(_ . id:id)
(define id-str (symbol->string (syntax-e #'id)))
(define components (string-split id-str "/"))
(match-define-values (mod-components (list mod-id-str))
(split-at-right components 1))
(define mod-path (string-join mod-components "/"))
(with-syntax ([mod-path-id (string->symbol mod-path)]
[mod-id (datum->syntax #'id (string->symbol mod-id-str))])
(syntax-local-introduce
(syntax-local-lift-require
(syntax-local-introduce #'mod-path-id)
(syntax-local-introduce #'mod-id))))]))
Now you can do something like this:
> (racket/base/list 1 2 3)
'(1 2 3)
Unfortunately, this won’t work for macros because unbound identifiers
handled by #%top cannot resolve to a macro identifier (more
specifically, the #%app introduced puts the produced syntax in an
expression context). I’m not sure if there’s a way around that, and
the explicit use of some sort of require transformer is definitely
much nicer. Just a little bit fun to see how easy it is to do
something like this. ;)
> On Jun 29, 2016, at 6:05 AM, Shakna Israel <[email protected]> wrote:
>
> I'm trying to introduce an implicit binding for require.
>
> One of the features I love about Python, is the namespace binding.
>
> import sys
>
> sys.stdout.write("Sweet")
>
> I know this can also be accomplished with require by specifying a prefix-in,
> or a prefix-out with a local-require.
>
> However, I want a require that does that for me.
>
> (require racket/base)
> (racket/base:println "It works!")
>
> Figuring the namespace mangling is simple enough.
>
> However, for this to work, I think I need a require transformer, but I'm at a
> loss. The only example I see can mess with the module path, but not the
> symbols it exposes.
>
> Any pointers?
>
> --
> 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 [email protected].
> For more options, visit https://groups.google.com/d/optout.
--
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 [email protected].
For more options, visit https://groups.google.com/d/optout.