Source

ocaml-pure-polyrecord /

Filename Size Date modified Message
68 B
694 B
20.5 KB
1.7 KB
4.0 KB
345 B
331 B
1.4 KB

Marshallable polymorphic record in OCaml

This is a quick implementation of polymorphic records in OCaml.

OCaml objects are actually polymorphic records but not safely marshallable since it contains a method table. This implementation does not use objects but hash tables to support marshalling.

How to build

As follows:

yes no | omake --install    # you need OMake
omake

It builds a library polyrecord.cma and CamlP4 syntax extension pa_polyrecord, then compiles test.ml using them.

No installation build command is available for now.

Syntax

Record creation:

{< field = value; ... >}

Field access:

record..field

Field update:

record..field <- expr

For simplicity, the poly-record is immutable by itself. If you want to have mutable fields, you must use reference cells explicitly. The field access syntax provides a sugar to update the reference:

let r = {< x = 1; y = ref None >}   (* x and y are immutable, but y is a ref which is mutable *)
let () = r..y <- Some 2             (* update the ref at y *)
let () = assert (!(r..y) = Some 2)  (* Accessing the ref content is a bit lousy... *)

Forget fields via coercion

The type system can forget fields using coercion:

let r = {< x = 1; y = None >}
let r' = (r  :>  < x : int > Polyrecord.t)  (* forget about y *)

Limitations

No easy way to add fields

It would be nice adding new fields at copy via { r with new_field = e }, but it seems impossible due to the typing of OCaml objects.

No pattern matching