[racket] exn:fail:contract:variable id field

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Fri Apr 1 08:45:55 EDT 2011

At Fri, 1 Apr 2011 04:10:19 -0500, Casey Klein wrote:
> What exactly is in the `id' field of a `exn:fail:contract:variable'
> structure? It doesn't seem to be the symbolic name of the
> not-yet-defined variable.

It's the real symbolic name --- the one that would work with
`namespace-variable-value' and similar reflective operations.

In this case, you get `x.6', because the original symbolic name `x' was
introduced by a macro; the variable name can't be just 'x, since it
must be renamed for hygiene. Also, the symbol that prints as 'x.6' in
this case is a kind of gensym --- not the result of `(string->symbol
"x.6")'.

The mismtach between `x' and `x.6' reflects is a mismatch between
identifiers (the modern Scheme way of binding) and symbols (the
traditional Lisp way of binding, on which the identifier layer is
currently built and that sometimes shows through in the reflection
API).

> #lang racket
> 
> (define-syntax (m stx)
>   (syntax-case stx ()
>     [(_ x)
>      (let ([y (datum->syntax #'fresh (syntax-e #'x) #'x)])
>        #`(begin
>            #,y
>            (define #,y 1)))]))
> 
> (uncaught-exception-handler
>  (λ (exn)
>    (displayln (exn:fail:contract:variable-id exn))
>    ((error-escape-handler))))
> 
> (m x) ; => x.6, not x




Posted on the users mailing list.