
Thanks!

Today’s question: Given some string or symbol, how to create an identifier that identifier-binding
will report is an existing module (not imported) binding in expanded syntax — and the module-path-index is actually the source file instead of #<module-path-index='\|expanded module\|>
a.k.a. (module-path-index-join #f #f)
. https://gist.github.com/greghendershott/e94e4aee7cb3040f5332d0b0b80800e5

This isn’t urgent, for my immediate need I have a work-around that I don’t think is too hacky. But, after re-reading some reference materials about bindings, module bindings, expansion, syntax-binding-set, I’m kind of bothered that I can’t figure out how to make such an identifier. So, curious.

Just to understand completely, you mean across all (locally hosted) packages that may not be loaded as of yet?

Oh. For my immediate need, no. Just one specific file.

It’s not like identifier-binding
is the only way to do this, with expanded syntax. You could just walk the syntax for define-values
etc. It’s just, identifier-binding
does give the correct answer when you’re in a module->namespace
as opposed to merely expanded syntax. And, it would be handy (for me) if it behaved the same way. So I know ways to work around this.

I guess I’m curious why identifier-binding
works one way with a module->namespace
namespace, but another way with a namespace in which the syntax has been expanded. And what is the story about the module-path-index in the latter case? (The #<module-path-index='\|expanded module\|>
a.k.a. (module-path-index-join #f #f)
). Is that just some weird corner case and/or tiny buglet, or, does it reflect something about expansion and module bindings that I don’t know?

A module declaration that is expanded or compiled doesn’t yet have a name. The module declaration might even be evaluated multiple times with different names. Meanwhile, module->namespace
always accesses a module that was created by evaluating a module declaration.

Ah, I see. That makes sense. Thanks!

I guess that also explains why I got the same result, even when I carefully found and extracted the exact piece of identifier syntax from the expanded syntax, and gave that to identifier-binding
. Even with that lexical context, same result.

> A module declaration that is expanded or compiled doesn’t yet have a name. The module declaration might even be evaluated multiple times with different names. > Wait, what? How do modules get their names?

A module gets a name via current-module-declare-name
, which is set by things that evaluate module declarations, such as loading them from a file.

In the REPL when I type the following: > (define x 100)
> (member x (namespace-mapped-symbols))
member
is able to find x
. But when I put the code into a file, member
returns #f
instead. Is there a way to get the REPL-like behavior in a file?

You’ll have to use some of the tools here: https://docs.racket-lang.org/reference/Namespaces.html?q=namespace-anchor#%28def._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._namespace-anchor-~3enamespace%29%29 and pass the namespace explicitly

Those commands give me #f
Welcome to Racket v7.6.0.9.
> (define x 100)
> (member x (namespace-mapped-symbols))
#f
But I think you want to make a namespace for your file: #lang racket/base
(define-namespace-anchor n)
(define x 100)
(and (member 'x (namespace-mapped-symbols (namespace-anchor->namespace n))) #t)

If you use 'x
in the first one it will work

Thanks! I’ll have to do some reading namespace anchors. edit: oops, samth’s comment made me realize that I typed x
instead of (what I meant),'x
.

Yeah that’s not great