combining require, build-path, and namespaces

57 views
Skip to first unread message

Shriram Krishnamurthi

unread,
Jul 21, 2020, 11:45:38 AM7/21/20
to Racket Users
How I can combine these three? I want to do something like this:

    (define n (make-base-namespace))
    (define p (build-path f))
    (eval `(require ,p) n)

Racket doesn't like that: bad syntax for require sub-form because p is a path-typed value.

Essentially, I want to inject the module at f into n so that the provided identifiers of f are visible inside n. (I haven't been able to get dynamic-require working either, nor is it an entirely satisfactory solution because I may not always know what names f is providing.)

Thanks,
Shriram

Jens Axel Søgaard

unread,
Jul 21, 2020, 12:05:32 PM7/21/20
to Shriram Krishnamurthi, Racket Users

Sounds like a job for `namespace-require` with a require spec of the form `(file ,(path->string (build-path f)))`.

/Jens Axel

Shriram Krishnamurthi

unread,
Jul 21, 2020, 2:05:22 PM7/21/20
to Jens Axel Søgaard, Racket Users
Thank you! Would you know why I might get this error:

; require: unknown module

;   module name:

;     #<resolved-module-path: <<correct path name>>


(This is from inside a module.)

Trying the same at the REPL, I see the same thing:

> (define n (make-base-namespace))

> (namespace-require `(file ,(path->string (build-path "wheats" "w1.rkt"))))


[note no optional namespace]
works fine; the name is available at the top-level; but using the same pathname but with the namespace parameter:

> (namespace-require `(file ,(path->string (build-path "wheats" "w1.rkt"))) n)

; require: unknown module

;   module name:

;     #<resolved-module-path: <<correct path name>>


(The file in question begins with #lang racket.)

Shriram

Sorawee Porncharoenwase

unread,
Jul 21, 2020, 2:25:17 PM7/21/20
to Shriram Krishnamurthi, Jens Axel Søgaard, Racket Users

This is weird. I usually parameterize current-namespace when using namespace-* functions, and in this particular case it works fine.


--
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/racket-users/CAJUf2yQijBugL3PBo%2B5SBj-9zEKnUc2ejH52VB0AkTW%3DniRGQw%40mail.gmail.com.

Jens Axel Søgaard

unread,
Jul 21, 2020, 2:29:34 PM7/21/20
to Sorawee Porncharoenwase, Shriram Krishnamurthi, Racket Users
Den tir. 21. jul. 2020 kl. 20.25 skrev Sorawee Porncharoenwase <sorawe...@gmail.com>:

This is weird. I usually parameterize current-namespace when using namespace-* functions, and in this particular case it works fine.


On Tue, Jul 21, 2020 at 11:05 AM Shriram Krishnamurthi <shr...@gmail.com> wrote:
Thank you! Would you know why I might get this error:

; require: unknown module

;   module name:

;     #<resolved-module-path: <<correct path name>>


(This is from inside a module.)

Trying the same at the REPL, I see the same thing:

> (define n (make-base-namespace))

> (namespace-require `(file ,(path->string (build-path "wheats" "w1.rkt"))))


[note no optional namespace]
works fine; the name is available at the top-level; but using the same pathname but with the namespace parameter:

> (namespace-require `(file ,(path->string (build-path "wheats" "w1.rkt"))) n)

; require: unknown module

;   module name:

;     #<resolved-module-path: <<correct path name>>


(The file in question begins with #lang racket.)



#lang racket

(define ns (make-base-namespace))
(define p  (build-path "/Users/soegaard/tmp" "w1.rkt"))

(parameterize ([current-namespace ns])
  (namespace-require p))

I had to dig up 

https://www.mail-archive.com/racket...@googlegroups.com/msg43291.html

to figure it out.

/Jens Axel

Greg Hendershott

unread,
Jul 21, 2020, 3:14:13 PM7/21/20
to Racket Users
Do you definitely want only definitions explicitly provide-ed by the module, or is it acceptable (or even desirable) to see all module definitions?  If the latter, you could use module->namespace.

Shriram Krishnamurthi

unread,
Jul 21, 2020, 10:38:23 PM7/21/20
to Jens Axel Søgaard, Sorawee Porncharoenwase, Racket Users
Ooh, thank you Oak and Jens Axel! I would never have figured that out.

As Matthew's email from Jan 2020 says, having the documentation say something (and, in particular, suggesting the use of `parameterize` to get what many users might expect) would be quite lovely.

(Thanks also, Greg.)

Shriram
Reply all
Reply to author
Forward
0 new messages