Skip to content

Instantly share code, notes, and snippets.

@FlandreDaisuki
Created August 23, 2021 15:46
Show Gist options
  • Save FlandreDaisuki/dbc5463df3ac5cc55ae2746746559dc7 to your computer and use it in GitHub Desktop.
Save FlandreDaisuki/dbc5463df3ac5cc55ae2746746559dc7 to your computer and use it in GitHub Desktop.
Why?
module Student = struct
type t = { id: int; name: string; }
let init (id, name) = { id = id; name = name; }
end
;;
let alice = Student.init(0, "Alice")
(* val alice : Student.t = {Student.id = 0; name = "Alice"} *);;
let () = Printf.printf "Hello, %s\n" alice.name
;;
let () = List.iter (fun s ->
Printf.printf "Hello, %s\n" s.name
(* ^^^^ *)
(* Error: Unbound record field name *)
) [alice]
;;
@FlandreDaisuki
Copy link
Author

OK. According to the module section of Real World OCaml, we need open the module signature.

We can expose it after module definition

module Student = struct
  type t = { id: int; name: string; }
  let init (id, name) = { id = id; name = name; }
end
;;

+ open Student;;

Or, expose it in minimum

- let () = List.iter (fun s ->
+ let () = let open Student in List.iter (fun s ->
  Printf.printf "Hello, %s\n" s.name
) [alice]
;;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment