ppx_poly_recordversion

PPX Polymorphic record

Polymorphic record in OCaml

This ppx adds polymorphic record.

`]>exp< record_poly%[

In side the extension [%poly_record ...], the record syntax is are changed from the normal (monomorphic) records to polymorphic records whose type is _ Ppx_poly_record.Poly_record.t.

Record creation `} .. ;e = l {

# [%poly_record { x = 1; y = 1.0 }];;
- : < x : int; y : float > Ppx_poly_record.Poly_record.t = <abstr>

Unlike the normal monomorphic records, it is not required to declare fields of the polymorphic records. They are inferred using OCaml's object type.

Field access `l.r

Accessing fields of the polymorphic records is by r.x inside [%poly_record ..]:

# [%poly_record fun r -> r.x];;
- : < x : 'tvar_1; .. > Ppx_poly_record.Poly_record.t -> 'tvar_1 = <fun>

Record copy with field updates: `} .. ;e = l with r {

The syntax of record copy { r with x = e } works for polymorphic records too:

# [%poly_record fun r -> { r with x = 1 }];;
- : (< x : int; .. > as 'a) Ppx_poly_record.Poly_record.t
    -> 'a Ppx_poly_record.Poly_record.t

Field mutation `e -< l.r

Field mutation r.x <- e works in [%poly_record ..], too, but a bit differently from the monomorphic record, since the polymorphic record has no type declaration to specify a field is mutable. In [%poly_record ..], r.x <- e works when the field x is a reference:

# [%poly_record let r = { x = ref 0 } in r.x <- 1; r];;
- : < x : int ref; .. > Ppx_poly_record.Poly_record.t = <abstr>

`].. record_mono%[

You can use [%mono_record ..] inside [%poly_record ..] to use the original monomorphic records.

Todo

  • Other extensions for other parts than expressions: [%%poly_record ..]
  • let [%poly_record] = true in ... or something equivalent
  • Patterns: { x = p }. This is diffcult and probably requires ppx_pattern_guard
AuthorJun Furuse
Published
Homepagehttps://bitbucket.org/camlspotter/ppx_poly_record
Issue Trackerhttps://bitbucket.org/camlspotter/ppx_poly_record/issues?status=new&status=open
Maintainerjun.furuse@gmail.com
Dependencies
Source [http] https://github.com/ocaml/opam-source-archives/raw/main/ppx_poly_record-1.0.1.tar.gz
sha256=f6e44445f5e04cf33c5fc6fc24f11c3be4824d8fbf63760f76ef729cbc3a3ac0
md5=14347831b78638f6e36aba0b6d4dc276
Edithttps://github.com/ocaml/opam-repository/tree/master/packages/ppx_poly_record/ppx_poly_record.1.0.1/opam
No package is dependent