package ppx_poly_record

  1. Overview
  2. No Docs
PPX Polymorphic record

Install

Dune Dependency

Authors

Maintainers

Sources

ppx_poly_record-1.0.1.tar.gz
md5=14347831b78638f6e36aba0b6d4dc276

Description

Polymorphic record in OCaml

This ppx adds polymorphic record.

[%poly_record <exp>]

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 { l = e; .. }

# [%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 r.l

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: { r with l = e; .. }

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 r.l <- e

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>

[%mono_record ..]

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

Published: 05 Nov 2014

Dependencies (3)

  1. omake build & < "0.10.1"
  2. ocamlfind build
  3. ocaml >= "4.02.1" & < "4.03.0"

Dev Dependencies

None

Used by

None

Conflicts

None