package ppx_tydi

  1. Overview
  2. Docs
Let expressions, inferring pattern type from expression.

Install

Dune Dependency

Authors

Maintainers

Sources

v0.17.0.tar.gz
sha256=8d22dc50f0ec75380b893063a2294555dc325d21777bc09d2c6e201b391e4265

README.mdx.html

README.mdx

"ppx_tydi: concise type-directed disambiguation of records in let-bindings"
===========================================================================

`ppx_tydi` allows concise type-directed disambiguation of record
patterns on the left-hand side of let-bindings:

<!-- $MDX file=test/test.mlt,part=simple-good -->
```ocaml
let () =
  let%tydi { foo } = r in
  ignore (foo : int)
;;
```

where an ordinary let-binding would give an error:

<!-- $MDX file=test/test.mlt,part=simple-bad -->
```ocaml
let () =
  let { foo } = r in
  ignore (foo : int)
;;

[%%expect
  {|
Line _, characters _-_:
Error: Unbound record field foo
|}]
```

`ppx_tydi` rewrites let-bindings to match statements, e.g.

<!-- $MDX file=doc/generate-snippets-for-readme/before.ml -->
```ocaml
let () =
  let%tydi (() as unit) = () in
  unit
;;
```

goes to

<!-- $MDX file=doc/generate-snippets-for-readme/before.ml.pp -->
```ocaml
let () = match () with | () as unit -> unit
```

`tydi` stands for "Type-directed" and is pronounced "Tidy".

The problem is that the compiler cannot use the type of the expression
on the right hand side to disambiguate the record pattern on the left.
Let-bindings with record patterns are checked from left to right.

Confusingly, let-bindings of variant patterns are fine. They get
type-checked from right to left. This is an implementation detail
related to type-checking of GADTs.
OCaml

Innovation. Community. Security.