Overlapping module imports in Racket
Asked Answered
S

1

11

I want to load an image and animate it in Racket. I can do it easily in the Dr. Racket, but I am using Emacs with Geiser. To load the image I need to:

(require racket/draw)

Next, to draw this image onto the screen, I'm planning to use the big-bang module. To load this module I have to:

(require 2thdp/image)

But I get this error:

module: identifier already imported from: 2htdp/image
at: make-pen
in: racket/draw
errortrace...:

This basically means that I can't import the same module twice. But I need both these libraries. How do I avoid this problem?

Scissor answered 27/7, 2013 at 6:15 Comment(0)
D
16

When two modules provide functions with the same name, you can rename the functions on import.

A simple way to do this is to rename all the functions from one of the modules, renaming all of them using some common prefix. You can do this with the prefix-in modifier to require:

(require racket/draw)
(require (prefix-in htdp: 2htdp/image))

make-pen      ; the `make-pen` from racket/draw
htdp:make-pen ; the `make-pen` from 2htdp

By the way, there is nothing special about the :, it's just a convention I've seen used. Instead of htdp: the prefix could be (say) htdp-. Whatever you use, it is prepended to every name provided by that module.

If just one function name conflicts, you could rename just that one function from one of the modules, using rename-in.

For more information see require.

Diligence answered 27/7, 2013 at 14:27 Comment(2)
Correct me if I'm wrong, but the tradition comes from CL's packages right?Tangelatangelo
So often I am surprised by how simple (at least for the user) and elegant Racket solves problems. This is another great example.Evie

© 2022 - 2024 — McMap. All rights reserved.