With the following type definition in ocaml:
type yearday = YMD of int * int * int
how would you access the different ints of the type? Like if I just wanted the value of the first int.
The parts of a value like this are accessed through pattern matching. Here's a function that returns the first int:
let y_of_ymd (YMD (y, _, _)) = y
Here's how it looks in the toplevel (OCaml REPL):
# let y_of_ymd (YMD (y, _, _)) = y;;
val y_of_ymd : yearday -> int = <fun>
# let myymd = YMD (2017, 4, 18);;
val myymd : yearday = YMD (2017, 4, 18)
# y_of_ymd myymd;;
- : int = 2017
#
Update
If you have more than one variant in your type you can use match to determine which kind of value is present
type yearday = YMD of int * int * int | YD of int * int
let y_of_yearday yearday =
match yearday with
| YMD (y, _, _) -> y
| YD (y, _) -> y
There are more concise ways to write this, but I think this gives the best idea of what's going on.