To add another perspective, the dot character is used for two different things here:
- Opening or specifying a module.
- Accessing a field of a record.
Lwt_unix open (its identifiers visible), “interpret”
However, the only identifier from
Lwt_unix in that expression is the record field name
ai_addr. So, the expression could have been
List.hd addresses. The result will be a record of some type (that we later see is from
Lwt_unix) that has an
ai_addr field. Then, get the value of that
ai_addr field. The
ai_addr field is defined in
I don’t believe the following is valid OCaml record syntax, but we could make this expression a bit clearer by inserting parentheses:
The dots have these meanings:
- The dot in
List.hd is the module path dot. It means refer to the identifier
hd in the module
- The dot in
Lwt_unix.ai_addr is also the module path dot. It means refer to the identifier
ai_addr in the module
- The middle dot is the record access dot. It means “look up” field
Lwt_unix.ai_addr in the record produced by the expression on the left of the dot.
The way you know which dot is of which kind is:
- If the thing to the left of the dot is a module, then it’s a module path dot.
- If the thing to the left is an expression, then it’s a record field access dot.
The module dot is easy to spot because the name to the left will be capitalized. Everything else is the record access dot.
EDIT: Actually, the dot is also used for string and array access, with syntax
e.(e). You detect this when you see the bracket or parenthesis (this is why one of the lines above wasn’t valid record access syntax).