[Feature Request] Improve error message for neglecting to (require (for-syntax racket/base))

See [Feature Request] Improve error message for neglecting to (require (for-syntax racket/base)) · Issue #3912 · racket/racket · GitHub
I do not understand why (require (for-syntax racket/base)) is required.
The following works:

#lang racket
(define-syntax aap (syntax 'noot))

According to the docs syntax syntax is exported from racket/base too,
but the following does not work:

#lang racket/base
(define-syntax aap (syntax 'noot))

With racket/base a require for syntax is required:

#lang racket/base
(require (for-syntax (only-in racket/base syntax)))
(define-syntax aap (syntax 'noot))

This puzzles me. How come?

2 Likes

It's due to phases.

The module racket/base exports syntax to phase 0.
The expression of (define-syntax id expr) is evaluated in phase 1.
So to import syntax for phase 1, an (require (for-syntax racket/base)) is needed.

On the other hand (if I remember correctly) racket exports syntax to both phase 0 and 1.

In practice when I use racket/base and need to define a macro, I always add:

(require (for-syntax racket/base racket/syntax syntax/parse))

With respect to the error message: The current error message simply says syntax is unbound.
It is possible to detect that is available in a different phase - so it could suggest to the user that an adding an import for the the correct phase might fix the problem.

2 Likes

Hello Soegaard
Thanks for your prompt and clear answer.
Saludos, Jos