# Syntactic sugar for field accessing?

**URL:** <https://discuss.ocaml.org/t/syntactic-sugar-for-field-accessing/10112>\
**Category:** Learning\
**Created:** [July 1, 2022, 3:05pm UTC](https://discuss.ocaml.org/t/syntactic-sugar-for-field-accessing/10112 "2022-07-01T15:05:59Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![bn-d](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/bn-d/32/6471_2.png) [@bn-d](https://discuss.ocaml.org/u/bn-d)\
**Post date:** [July 1, 2022, 3:05pm UTC](https://discuss.ocaml.org/t/syntactic-sugar-for-field-accessing/10112/1 "2022-07-01T15:05:59Z")

</div>

Is there any syntactic sugar for record field accessor?

I.e for

```ocaml
module T = struct
  type t = {
    value : int
  }
end

```

instead of doing

```ocaml
let value_list = List.map (fun t -> t.T.value) t_list

```

we do similar to

```ocaml
let value_list = List.map (.T.value) t_list

```

If there is no such thing in OCaml, is it a good idea to add it to the language?

---

<div class="post-metadata">

**Author:** ![dra27](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/dra27/32/5294_2.png) [@dra27](https://discuss.ocaml.org/u/dra27)\
**Post date:** [July 1, 2022, 3:24pm UTC](https://discuss.ocaml.org/t/syntactic-sugar-for-field-accessing/10112/2 "2022-07-01T15:24:35Z")

</div>

> [@bn-d](#):
>
> Is there any syntactic sugar for record field accessor?

Not directly, but you can use [ppx\_fields\_conv](https://opam.ocaml.org/packages/ppx_fields_conv) which allows you to write:

```ocaml
module T = struct
  type t = {
    value : int
  } [@@deriving fields]
end

```

and defines, amongst other things,

```ocaml
val value : t -> int

```

allowing:

```ocaml
let value_list = List.map T.value t_list

```

> [@bn-d](#):
>
> If there is no such thing in OCaml, is it a good idea to add it to the language?

Related suggestions have been made before (e.g. [ocaml/ocaml#9005](https://github.com/ocaml/ocaml/pull/9005))

---

<div class="post-metadata">

**Author:** ![hyphenrf](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/hyphenrf/32/2733_2.png) [@hyphenrf](https://discuss.ocaml.org/u/hyphenrf)\
**Post date:** [July 1, 2022, 4:05pm UTC](https://discuss.ocaml.org/t/syntactic-sugar-for-field-accessing/10112/3 "2022-07-01T16:05:38Z")

</div>

that would be so nice to have, especially since currently `(.ident)` is an illegal form.

---

<div class="post-metadata">

**Author:** ![yawaramin](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/yawaramin/32/3384_2.png) [@yawaramin](https://discuss.ocaml.org/u/yawaramin)\
**Post date:** [July 1, 2022, 5:37pm UTC](https://discuss.ocaml.org/t/syntactic-sugar-for-field-accessing/10112/4 "2022-07-01T17:37:43Z")

</div>

In general the answer to ‘should there be xyz syntax sugar’, should be, ‘Think very carefully if saving a few characters is worth the added maintenance burden forever’ 🙂

---

<div class="post-metadata">

**Author:** ![art-w](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/art-w/32/4728_2.png) [@art-w](https://discuss.ocaml.org/u/art-w)\
**Post date:** [July 1, 2022, 8:00pm UTC](https://discuss.ocaml.org/t/syntactic-sugar-for-field-accessing/10112/5 "2022-07-01T20:00:39Z")

</div>

I don’t love it.

But it’s really hard to talk about syntaxic sugar without being subjective 🙂 . Would others actually use the sugar if it was added? I think we can get an idea by grepping the existing code published on opam:

- There are [838 examples](https://sherlocode.com/?q=%28fun+%5C%5Ba-z%5C%5D%5Cw%5C%2B+-%3E+%5C%5Ba-z%5C%5D%5Cw%5C%2B.%5C%5B%27a-zA-Z0-9_.%5C%5D%5C%2B%29) of lambdas used only to access a record field that could benefit from the sugar, for a codebase that contains [over 500k usages of the `fun` keyword](https://sherlocode.com/?q=%5Cbfun%20). It’s not nothing, but it’s not much.

- But perhaps adding the sugar would encourage people to use it more! (do we really want that?) For context, the `( >>= )` operator is still used [~50k times](https://sherlocode.com/?q=%3E%3E%3D) while `let*` has only been adopted on [~10k lines](https://sherlocode.com/?q=let*). The [new `.%()` operators](https://sherlocode.com/?q=.%5C%5B-%3F%21%2B%40*%25%24%26%5C%5D%5C%5B%28%5B%7B%5C%5D) aren’t really popular. It takes a lot of time and motivation to introduce new syntax.

I’m not saying that lesser used features are bad though! But that this specific sugar wouldn’t make a huge difference… in my opinion.

(These links are neither uptodate nor representative of all the OCaml code ever written… And the counts are approximate, you need to click the “More” button a few times for it to converge to a better answer: it’s only exact if you manage to hit the last page.)

---

<div class="post-metadata">

**Author:** ![Chet\_Murthy](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/chet_murthy/32/1501_2.png) [@Chet\_Murthy](https://discuss.ocaml.org/u/Chet_Murthy)\
**Post date:** [July 1, 2022, 8:15pm UTC](https://discuss.ocaml.org/t/syntactic-sugar-for-field-accessing/10112/6 "2022-07-01T20:15:21Z")

</div>

> [@yawaramin](#):
>
> ‘Think very carefully if saving a few characters is worth the added maintenance burden forever’

This is one of the pluses of PPX extensions for this sort of thing: you shift the burden of maintenance from the compiler writer (and maintainer) to the user of the PPX extension (and, sure, the maintainers of the extension, if it’s popular enough).

---

<div class="post-metadata">

**Author:** ![bn-d](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/bn-d/32/6471_2.png) [@bn-d](https://discuss.ocaml.org/u/bn-d)\
**Post date:** [July 2, 2022, 12:03am UTC](https://discuss.ocaml.org/t/syntactic-sugar-for-field-accessing/10112/7 "2022-07-02T00:03:42Z")

</div>

Thanks! I will check the issue.

---

<div class="post-metadata">

**Author:** ![bn-d](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/bn-d/32/6471_2.png) [@bn-d](https://discuss.ocaml.org/u/bn-d)\
**Post date:** [July 2, 2022, 1:19am UTC](https://discuss.ocaml.org/t/syntactic-sugar-for-field-accessing/10112/8 "2022-07-02T01:19:36Z")

</div>

Some really nice data!

- But I’d argue people writing this in different ways. i.e

```ocaml
(fun {value} -> value)

```

or named

```ocaml
let value t = value

```

or as `dra27` point outed above, indirectly through `[@@deriving fields]`.

- And as you said, new sugar would effect how people write codes. i.e. `let*` actually significantly changed how I write `Lwt`. I found `let*` being very useful. I think people will use this more if easier to write.

- Will this make a huge difference. Obviously no. This is only a syntactic sugar. I’d also argue this following syntactic sugar does not make a huge difference. But that is some of the most important syntactic sugar. I even found `let*` more useful for my use cases.

```ocaml
let my_fun = (fun var1 -> ...)
(* vs *)
let my_fun var1 = ...

```

---

<div class="post-metadata">

**Author:** ![hyphenrf](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/hyphenrf/32/2733_2.png) [@hyphenrf](https://discuss.ocaml.org/u/hyphenrf)\
**Post date:** [July 2, 2022, 10:37am UTC](https://discuss.ocaml.org/t/syntactic-sugar-for-field-accessing/10112/9 "2022-07-02T10:37:57Z")

</div>

I’ve recently found out that only ~0.3% of functions are partially-applied in a more partial-application-happy language (Haskell), [here’s the source](http://simonmar.github.io/bib/papers/eval-apply.pdf) (the journal version has more code samples and reaches similar numbers), yet curried functions are the default in our stdlib, and significant work was put into ensuring curried function application is efficient in OCaml. I’d say it’s not always a matter of “how often X is used”, more like “how nice X is when you need it”.

---

<div class="post-metadata">

**Author:** ![nojb](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/nojb/32/519_2.png) [@nojb](https://discuss.ocaml.org/u/nojb)\
**Post date:** [July 2, 2022, 11:17am UTC](https://discuss.ocaml.org/t/syntactic-sugar-for-field-accessing/10112/10 "2022-07-02T11:17:48Z")

</div>

> [@hyphenrf](#):
>
> yet curried functions are the default in our stdlib, and significant work was put into ensuring curried function application is efficient in OCaml. I’d say it’s not always a matter of “how often X is used”, more like “how nice X is when you need it”.

Apologies for going off-topic, but the choice for using curried instead of n-ary or tupled functions in OCaml is argued in detail in the [ZINC paper](https://xavierleroy.org/publi/ZINC.pdf) (see p13) and it has to do with “how nice it is”, but even more so with the fact that there is no clear agreement as to how n-ary functions should be integrated with ML-style typing discipline.

Cheers,  
Nicolas

---

<div class="post-metadata">

**Author:** ![Chet\_Murthy](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/chet_murthy/32/1501_2.png) [@Chet\_Murthy](https://discuss.ocaml.org/u/Chet_Murthy)\
**Post date:** [July 2, 2022, 4:50pm UTC](https://discuss.ocaml.org/t/syntactic-sugar-for-field-accessing/10112/11 "2022-07-02T16:50:03Z")

</div>

To go further, “supporting curried functions well” isn’t a mere question of syntax: it’s a question of _efficiency_. @nojb is right to reference the original ZINC paper (a lovely work).

But really, I don’t understand what the problem is: if you want support for `.foo` as a shorthand for `(fun x -> x.foo`, you can get it: just add a minor extension to the OCaml grammar in Camlp5 and you’re done.  
But the thing is, it was widely agreed in the OCaml community that such hacking on the grammar of OCaml was a bad idea, and the result of that agreement was PPX.

Why not just add the syntax support and see if enough people actually use it? (which I kinda doubt, b/c `ppx_fields_conv` is already good enough).

[this is not a plug for Camlp5, but rather, pointing out that if you’re actually serious about wanting new syntax, you can get it without asking anybody else to do the _work_.]

---

<div class="post-metadata">

**Author:** ![bn-d](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/bn-d/32/6471_2.png) [@bn-d](https://discuss.ocaml.org/u/bn-d)\
**Post date:** [July 8, 2022, 1:06am UTC](https://discuss.ocaml.org/t/syntactic-sugar-for-field-accessing/10112/12 "2022-07-08T01:06:52Z")

</div>

Inspired by [ocaml/ocaml#9005](https://github.com/ocaml/ocaml/pull/9005), I created a proposal for the exact syntax I mentioned here.

> <https://github.com/ocaml/ocaml/pull/11407>
>
> Hi,
> 
> I'd like to prepose this simple syntactic sugar for record field accessin…g
> 
> \`\`\`ocaml
> type record = { field : field\_type }
> 
> (.field)
> \- : record -\> field\_type = \<fun\>
> \`\`\`
> This syntactic sugar can help users map record to field in all kinds of operations. This vs direct field accessing is similar to \`function\` vs \`match\` imo. This can make some codes much cleaner without adding too much to the language syntax.
> \`\`\`ocaml
> let field\_list = List.map (.field) record\_list
> (\* vs \*)
> let field\_list = List.map (fun t -\> t.field) record\_list
> \`\`\`
> 
> \`\`\`ocaml
> let f input = input |\> some\_operations |\> (.value)
> (\* vs \*)
> let f input = input |\> some\_operations |\> fun res -\> res.value
> \`\`\`
> 
> \#### Implementation
> 
> This is implemented using a new \`Pexp\_fieldfun of Longident.t loc\` expression and will be rewritten to \`Texp\_function\` in \`typecore\`. It will be equivalent to the following expression.
> \`\`\`ocaml
> function | record -\> record.field\_name
> \`\`\`
> 
> The parentheses are required and a part of the syntax to make this look like operators.
> 
> \#### Future works
> Nested field accessing ie \`(.field.sub\_field)\` is currently not supported. And will get \`Syntax error\` on compile. But I believe this can be implemented by extending \`Pexp\_fieldfun\` to accept \`Longident.t loc list\` in future works.
> 
> \#### Related
> https://github.com/ocaml/ocaml/pull/9005 : Promote constructors to functions
> 
> Please feel free to leave any comment/suggestion. Thanks!

---

<div class="post-metadata">

**Author:** ![yawaramin](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/yawaramin/32/3384_2.png) [@yawaramin](https://discuss.ocaml.org/u/yawaramin)\
**Post date:** [July 8, 2022, 1:50am UTC](https://discuss.ocaml.org/t/syntactic-sugar-for-field-accessing/10112/13 "2022-07-08T01:50:17Z")

</div>

Cool. If the mentioned nested record field access and setter syntax are actually implemented, then this becomes a pretty serviceable lens syntax.

---

<div class="post-metadata">

**Author:** ![Chet\_Murthy](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/chet_murthy/32/1501_2.png) [@Chet\_Murthy](https://discuss.ocaml.org/u/Chet_Murthy)\
**Post date:** [July 9, 2022, 9:58pm UTC](https://discuss.ocaml.org/t/syntactic-sugar-for-field-accessing/10112/14 "2022-07-09T21:58:15Z")

</div>

I certainly have no dog in this fight (and since I maintain and use Camlp5, I’m a general fan of syntax extension) but I did wonder: how much of this could be achieved easily, by modular implicits? It seems like … a lot ?

Just curious, not making a point of any sort.
