# Ocaml record type usage

**URL:** <https://discuss.ocaml.org/t/ocaml-record-type-usage/4359>\
**Category:** Learning\
**Created:** [September 12, 2019, 10:45am UTC](https://discuss.ocaml.org/t/ocaml-record-type-usage/4359 "2019-09-12T10:45:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![BikalGurung](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/bikalgurung/32/5105_2.png) [@BikalGurung](https://discuss.ocaml.org/u/BikalGurung)\
**Post date:** [September 12, 2019, 10:45am UTC](https://discuss.ocaml.org/t/ocaml-record-type-usage/4359/1 "2019-09-12T10:45:23Z")

</div>

I just found out that the following code to access a record field is valid,

```auto
module A = struct type a = {field1:string;field2:int} end;;
let a = {A.field1 = "hello"; field2 = 100};;

a.field1 = a.A.field1;; (* this is valid, i.e. a.A.field1 *)

```

Is this documented somewhere in the manual?

---

<div class="post-metadata">

**Author:** ![octachron](https://avatars.discourse-cdn.com/v4/letter/o/49beb7/32.png) [@octachron](https://discuss.ocaml.org/u/octachron)\
**Post date:** [September 12, 2019, 12:16pm UTC](https://discuss.ocaml.org/t/ocaml-record-type-usage/4359/2 "2019-09-12T12:16:20Z")

</div>

It is documented in the technical description of the language: [https://caml.inria.fr/pub/docs/manual-ocaml/expr.html#sec150](https://caml.inria.fr/pub/docs/manual-ocaml/expr.html#sec150) . But it seems that indeed qualified record fields are not described in the tutorial part of the manual.

---

<div class="post-metadata">

**Author:** ![lindig](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/lindig/32/532_2.png) [@lindig](https://discuss.ocaml.org/u/lindig)\
**Post date:** [September 15, 2019, 11:11am UTC](https://discuss.ocaml.org/t/ocaml-record-type-usage/4359/3 "2019-09-15T11:11:25Z")

</div>

> [@BikalGurung](#):
>
> module A = struct type a = {field1:string;field2:int} end;;

You can also write

```auto
let a = A.{field1 = "hello"; field2 = 100};;

```

which is often more convenient as you don’t have to repeat the module name.

---

<div class="post-metadata">

**Author:** ![Leonidas](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/leonidas/32/4039_2.png) [@Leonidas](https://discuss.ocaml.org/u/Leonidas)\
**Post date:** [September 16, 2019, 2:39pm UTC](https://discuss.ocaml.org/t/ocaml-record-type-usage/4359/4 "2019-09-16T14:39:45Z")

</div>

Though this has a bit different semantics. In this case it makes no difference since the code uses only literals, but in this code:

```ocaml
let open A in
{field1 = "hello"; field2 = value}

```

it would try to use `A.value` if there was one defined. Which is an easy way to get tripped up.
