1
votes

I can define a class like so in a racket repl that I start from my shell. I'm using v6.12

(define book-class%
  (class object%
    (field (pages 5))
    (define/public (letters)
      (* pages 500))
    (super-new)))

Everything works fine. But when I try the same in emacs, executing the expression via C-x C-e, the output is

; class: undefined;
;  cannot reference undefined identifier

Writing the same definition directly in the emacs racket shell shows the same error.

Why is this happening and how can I change my repl to allow the creation of classes?

1
You probably have to import racket/class by writing (require racket/class).Alexis King
That import does the trick. Any idea why the emacs repl requires it, but the racket repl started from bash might not?munk
It is because racket/class is part of racket, but not racket/base. An as far as I know racket-mode in Emacs defaults to racket/base.soegaard

1 Answers

3
votes

The "racket-mode" package is really multiple modes. The main two are:

  • racket-mode for editing Racket source files.
  • racket-repl-mode a REPL.

If you're doing M-x racket-repl, then racket-repl-mode opens directly. It is associated with no racket-mode buffer or .rkt file. It is "empty".

As soegaard said, it has only bindings from racket/base.

As Alexis said, you could then enter (require racket/class) -- or (require racket) -- if you want to use things from racket/class.


Although you could use racket-repl-mode "stand-alone" the way you described, the intended use is a work flow like:

  1. Visit a .rkt file (which will automatically be a racket-mode buffer).
  2. C-c C-c a.k.a. M-x racket-run.
  3. Now you have a racket-repl-mode buffer with a prompt inside the namespace of the .rkt file.

Even if you care only about using the REPL stand-alone, you could make a little .rkt file to use to open the REPL. You could call it (say) config.rkt. Maybe it is simply:

#lang racket

Plus any requires you typically use.

Now you can start the REPL by opening config.rkt (or whatever you called it) and doing C-c C-c.