[racket] Duplicate definition of identifiers allowed in interactions window

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Sat Nov 19 10:38:08 EST 2011

At Thu, 17 Nov 2011 21:10:29 -0500, Harry Spier wrote:
> Dear list members,
> 
> While playing around with Racket in DrRacket, I accidently  entered in the
> interactions window.
> > (define x 7)
> > (define x 8)
> 
> and DrRacket allowed it, i.e.  I didn't get a "duplicate definition of
> identifiers error message.

REPL interactions --- and namespace-based evaluation in general ---
allow redefinitions, as long as a redefinition doesn't conflict with
the constantness of a binding. This "top-level" behavior is traditional
in Lisps, and it is useful for certain exploration and development
styles, so Racket supports it where practical.

> So to try and figure out what was going onI then entered this in the
> definitions window of DrRacket::
> *Code snippet A*
> (define-namespace-anchor a)
> (define ns (namespace-anchor->namespace a))
> (eval '(begin (define x 7) (define x 8)) ns)
> (eval 'x ns)
> 
> and still no "duplicate definitions error".

A top-level definition is never treated as a constant definition, so
that's why you can redefine `x' in this example.

> I then tried this:
> *Code snippet B*
> (define x 9)
> (define-namespace-anchor a)
> (define ns (namespace-anchor->namespace a))
> (eval '(begin (define x 7) (define x 8)) ns)
> (eval 'x ns)
> 
> and I got the error message: "cannot redefine a constant x"..

The `(define x 9)' within the module in this case makes `x' a constant
(since `x' is not `set!'ed within the body of the module), so you
cannot redefine `x' in this case. Note that the `(define x 7)' fails,
even before `(define x 8)'.



Posted on the users mailing list.