Suppose that I have the following record:
type t = {a:int}
In order to select the values of field a from a list I do the following:
let x = [{a=1};{a=2}]
let y = List.map (fun t -> t.a) x
This is a bit “unclean” to me. As a comparison, In Haskell I would do the following:
data T = T { a :: Int}
x = [T {a = 1}, T {a = 2}]
y = map a x
Is there a way to write something similar in Ocaml (maybe using external libraries)?
If not possible can someone explain why this limitation?
If you use Jane Street’s fieldslib library, available with core, then you can write:
and that gives you
aandbas selector functions of typet -> intIt also gives you a submodule called
Fieldsof so-called first-class fields, which include the selector, the mutator, and, critically for some applications, the string name of the field. This is very useful for generating good error messages in validations functions, for example.You also get some higher-order functions on the entire record, most usefully a fold over the record.