Why does clarifying the namespace lead to unbound error but not adding the namespace doesnt

let file = "example.dat"
let message = "Hello!"

let () =
  (* Write message to file *)
  let oc = open_out file in
  (* create or truncate file, return channel *)
  Printf.fprintf oc "%s\n" message;
  (* write something *)
  close_out oc;

  (* flush and close the channel *)

  (* Read file and display the first line *)
  let ic = open_in file in
  try
    let line = input_line ic in
    (* read line, discard \n *)
    print_endline line;
    (* write the result to stdout *)
    flush stdout;
    (* write on the underlying device now *)
    close_in ic
    (* close the input channel *)
  with e ->
    (* some unexpected exception occurs *)
    close_in_noerr ic;
    (* emergency closing *)
    raise e

I have this snippet of code from official ocaml site, if I annotate the functions using
in_channel.open_out, in_channel.open_in then while building I get the error unbound module in_channel but if I just use open_out etc like above it compiles. I am confused about this seemingly unintuitive behavior can someone explain . I am using dune to build it. Thanks.

Those functions are not in the In_channel module. See here: OCaml library : In_channel

They are in the Stdlib module. Eg: OCaml library : Stdlib

The Stdlib module is opened automatically, so you don’t need to prefix items from that module.

Also, In_channel/Out_channel were added in OCaml 4.14. You might be using an older version of OCaml. It would be best to upgrade to the latest available release, 5.2.1.

1 Like

Thanks Yawar. I got confused as there is the module in_channel and in writeup I was referring to they create an “in_channel” via Stdlib and some other code snippet I was looking they opened the file using In_channel, bunch of similar sounding ideas and names.