Newbie question: Unbound module Uchar.Utf8

I am completely new to Ocaml and I am trying to convert between utf8 and unicode code points. But unfortunately I am already stuck at calling the functions in the modules correctly. This is my code:

open Core
let () =
let c = Uchar.of_scalar 0x20ac in
match c with
| Some v → Printf.printf “item is %s\n” (Uchar.Utf8.to_string v)
| None → Printf.printf “Nothing”

The compiler complains about “Unbound module Uchar.Utf8”.
What am I doing wrong?
Thank you in advance for your help!
markusr

I am not a user of Core, but I suspect you have an old version of Core.

The latest versions (v0.17.0 and v0.17.1) have Core.Uchar.Utf8. Earlier versions do not.

You can see this by visiting https://ocaml.org/p/core/latest/doc/Core/Uchar/Utf8/index.html and navigating the versions in the top left corner.

Without more information about how you installed Core, you could try:

opam update
opam upgrade core

@jbeckford: You were right of course! My installation is just two days old, so I automatically checked the latest docs of Core and did not realise, that on my platform v0.17.x is not available yet. Thanks for your help and for the quick response.

Incidentally, note that you can do this using just the standard library:

# let utf8_of_scalar (n : int) : string =
    let buf = Buffer.create 0 in
    Buffer.add_utf_8_uchar buf (Uchar.of_int n);
    Buffer.contents buf
;;
val utf8_of_scalar : int -> string = <fun>
# utf8_of_scalar 0x20ac;;
- : string = "€"

Cheers,
Nicolas

2 Likes

You can even avoid using a Buffer:

# let uchar_to_utf_8 u =
    let b = Bytes.create (Uchar.utf_8_byte_length u) in
    ignore (Bytes.set_utf_8_uchar b 0 u);
    Bytes.unsafe_to_string b
  ;;
val uchar_to_utf_8 : Uchar.t -> string = <fun>
# uchar_to_utf_8 (Uchar.of_int 0x20AC);;
- : string = "€"
2 Likes

Thanks @nojb, @dbuenzli. It helps to know, that i could do this with the standard lib. Thx again.