
Racket news certificate seems to be out of date. Chrome is complaining.

I’ve emailed Paulo.

@spdegabrielle Thanks.

Mirroring using https is perfect. As for the email address, it would be indeed better to send messages to as the research one is not actively in use anymore. Thanks.

Hello! I am writing a macro that receives names of variables that are possibly defined at runtime. I want the macro to display
their values. In some cases, the macro looks up variables with getenv
and in some cases I want it to evaluate the variables by name. I am having trouble with the latter. I’ve been trying to use namespace-variable-value
, but I don’t fully understand what it does.
Why does (let ([a 5]) (displayln (namespace-variable-value 'a)))
give me a namespace-variable-value: given name is not defined
error, whereas (begin (define a 5) (namespace-variable-value 'a))
works fine?
This is what my macro currently looks like: (define-syntax (get-rash-syntax-repr stx)
(syntax-case stx (syntax quote)
[(_ (syntax (quote thing)))
#'thing]
[(_ varname)
#`(let* ([str-varname (symbol->string (syntax->datum varname))]
[lookup (getenv str-varname)])
(cond [(string-prefix? str-varname "$")
(namespace-variable-value
(string->symbol (substring str-varname 1)))]
[lookup lookup]
[else str-varname]))]))
Thank you!

The a
in the let-expression is a local variable.

The definition on the other hand is at the module top-level.

I see

In Racket you can’t get an environment that contains the locally bound variables.

It allows the compiler to generate faster code.

thank you

I’m now wondering how to do what I’m trying to do

An example is (let ([a 1]) (+ a 2)).

A compiler might rewrite this to (+ 1 2) and to 3. Thus not even allocating a variable at runtime.

right

Is the variable name known at compile time?

its name/symbol is, but its value is not

If the name is you can expand to #’varname since that will give you a variable reference.

That’s what I tried to do originally. The issue is the variable name will have a $
prepended to it. I think I tried the equivalent of #`#,(substring (symbol->string (syntax-datum #'varname)) 1)
but that didn’t work either.

I think we can fix that.

#lang racket
(require (for-syntax syntax/parse racket/syntax))
(begin-for-syntax
(define (remove-dollar id)
(define $name (symbol->string (syntax->datum id)))
(define name (substring $name 1 (string-length $name)))
(format-id id name)))
(define-syntax (lookup stx)
(syntax-parse stx
[(_lookup $name)
(with-syntax ([name (remove-dollar #'$name)])
#'name)]))
(let ([a 42])
(lookup $a))

Here (format-id id name)
produces an identifier name
with the same lexical context as id
.

cool!

I think this gives me what I need. Thank you!