How to require with prefix in Typed Racket?

Untyped racket has (require (prefix-in xml: xml)). Typed racket has (require/typed xml [ #| lots of decls here |# ]). How do I "mix" them?

2 Likes

Hi @charmonium and welcome to Racket!

It would be helpful if you could provide a minimal example code showing what you are trying to achieve, and highlighting the errors that prevent you from achieving your goal.

If you are trying to use xml with a prefix from typed code, the following should work:

#lang typed/racket
(require (prefix-in xml: typed/xml))

You have already written the line for requireing xml wth a prefix in untyped code.

1 Like

Is typed/xml found in the manual. I couldn't find it?

I can't find it in the docs either, but it is defined here:

2 Likes

I found it because I've recently worked with typed/pict, so I typed "typed/pict" in the search box, which lead me me to this page. I clicked on typed-racket-more at the end of the first line, which lead me to the of the typed-racket-more package, giving an explicit list of typed interfaces to different untyped libraries provided by this package.

1 Like

I had been about to suggest that 3 Libraries Provided With Typed Racket needed an update. It can be done at typed-racket/typed-racket-doc/typed-racket/scribblings/reference/libraries.scrbl at master · racket/typed-racket · GitHub

2 Likes

Setting aside the choice of typed/xml vs. xml, I realized I also did not know the answer to the original question.

I found that this works:

#lang typed/racket
(module untyped racket/base
  (define five 5)
  (provide five))
(require/typed
 (prefix-in u: 'untyped)
 [u:five Byte])
(add1 u:five) ;-> 6

However, it's not completely clear to me if it is supposed to work: I've opened `require/typed` duplicates effects from expanding `m`; docs unclear · Issue #1378 · racket/typed-racket · GitHub to follow up.

One thing I forgot to mention that Philip's suggestion reminded me of:

If you are going to use xml quite a bit in Typed Racket (and there wasn't typed/xml), I recommend the following:

  • Create a "typed-xml.rkt" module that does (require/typed xml …) and re-provides the functionality of the xml module.
  • Then, (typed) clients choose how to require it in the usual way, including (require (prefix-in xml: "typed-xml.rkt")).
2 Likes