Racket REPL and Submodules
Asked Answered
C

2

7

Is there an easy way for me to load a submodule in the current file in racket-mode in emacs?

For example if I have the following file

#lang racket

(define (foo x)
  x)

(module+ sub
  (define (bar x y)
    x))

and I hit f5 in racket-mode to start the repl then foo is available but bar is not.

Closefitting answered 24/8, 2015 at 15:3 Comment(0)
I
5

You can combine dynamic-enter! and quote-module-path to do this.

Given a repl interaction for the code above that you posted:

> (require racket/enter syntax/location)
> (dynamic-enter! (quote-module-path sub))
> bar
#<procedure:bar>

Alternatively, you could use dynamic-require/expose (the expose part allows you require things that are not provided), as done here.

Intermediate answered 25/8, 2015 at 14:51 Comment(3)
Thanks for answering this, Leif! I just now opened issue 151 for this.Punctuality
I just merged a commit to master, should flow through to MELPA shortly. racket-run now runs the innermost submodule enclosing point.Punctuality
I should mention that the module definitions will not be available if you are using (1) module+, (2) errortrace i.e. racket-error-context other than 'low or 'medium, and (3) Racket 6.2.1 or older. However it works if any of those conditions are absent. TL;DR it always works on Racket HEAD.Punctuality
Z
0

It works the same way in DrRacket. You have to provide bar from the submodule, and require the submodule to use it. Try the following code:

#lang racket

(define (foo x)
  x)

(module+ sub
  (define (bar x y)
    x)
  (provide bar))

;; (bar 1 2) -- undefined

(require (submod "." sub))
(bar 1 2) ;; -- works here
Zebe answered 25/8, 2015 at 9:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.