type node = {
lan: string;
lat: string;
};;
let rec read_nodes_from_list list = match list with
| Xml.Element("node", _, _)::list' -> {lan="A"; lat="B"}::read_nodes_from_list list'
;;
I tried this to create a node record but it doesn’t work. And suppose I have another type that has same attributes of node, how can I tell ocaml which type object to create?
Thank you.
Obviously, your function didn’t work because you forgot to match with empty list:
What you’re actually trying to do is a
mapoperation on list, so your function could be written more elegantly as follows:However, the function may not work because pattern matching on
Xml.Elementis not exhaustive. You should be careful when handling remaining cases. For example, something like this would work:To answer your question about record types, it considers a bad practice to have two record types with the same field label. You still can put these record types in different submodules and distinguish them using module prefixes. But as I said, having two similar record types in the same module causes confusion to you and the OCaml compiler.