[plt-scheme] "identifier used out of context" error

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Mon Sep 9 08:46:03 EDT 2002

> I'm writing a little procedure reflection package, that lets you
> inspect the source code of a closure and query the current values of
> its free variables.  But I ran across an error I don't understand...
> Here's the code (suggestions on how to improve it are welcome):
>
> [...]
>  (define (free-vars stx)
>     (let syntax-loop ((stx (expand stx)))
> [...]
> 
> But when I try to query a non-top-level variable, I get this error:
> 
> > (define forty-two (let ((x 42)) (lambda () x)))
> STDIN::216: compile: identifier used out of context in: x

The problem is that `expand' is a top-level expand. It doesn't work on
syntax extracted from within a lexical context. In particular, when
macro expansion starts to work on `(lambda () x)', the `x' has a
binding that isn't from the top level.

The solution is to use `local-expand', which is for expanding a
sub-expression during macro expansion:

  (local-expand stx 'expression null)

Matthew




Posted on the users mailing list.