Search code examples
schemerackethtdp

Overlapping module imports in Racket


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?


Solution

  • 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.