Import Overlapping Modules into Racket

I want to upload an image and animate it in Racket. I can do it easily in Dr. Racket, but I use Emacs with Geiser. To upload an image, I need:

(require racket/draw) 

Then, to draw this image on the screen, I plan to use the Big-Bang module. To download 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 cannot import the same module twice. But I need both of these libraries. How to avoid this problem?

+7
scheme racket
source share
1 answer

When two modules provide functions with the same name, you can rename functions when importing.

An easy way to do this is to rename all the functions from one of the modules, renaming them all 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 in : this is just the agreement I saw. Instead of htdp: prefix can be (say) htdp- . No matter what you use, it is added to every name provided by this module.

If only one function name conflicts, you can rename only one function from one of the modules using rename-in .

See require more details.

+11
source share

All Articles