ppx extension to ease the use of custom access operator across ocaml versions
  • OCaml 97.6%
  • Makefile 2.4%
Find a file
2016-01-27 17:50:06 +02:00
tests Packaged version 2015-04-08 09:53:47 +02:00
.merlin Merlin file 2016-01-27 17:42:12 +02:00
_tags Code cleaning + very basic documentation 2015-04-08 09:54:53 +02:00
descr Code cleaning + very basic documentation 2015-04-08 09:54:53 +02:00
license Code cleaning + very basic documentation 2015-04-08 09:54:53 +02:00
Makefile Code cleaning + very basic documentation 2015-04-08 09:54:53 +02:00
META Install file added + META update 2015-04-08 10:00:06 +02:00
opam Code cleaning + very basic documentation 2015-04-08 09:54:53 +02:00
ppx_indexop.install Install file added + META update 2015-04-08 10:00:06 +02:00
ppx_indexop.ml Update after index operators revert in trunk 2016-01-27 17:50:06 +02:00
Readme.md Update after index operators revert in trunk 2016-01-27 17:50:06 +02:00

ppx_indexop is a simple ppx rewriter which provides an unified way to write custom access operators for array-like type for ocaml versions ≥4.02.

For instance, bigarray-like operators (i.e., .{} and .{}<- ) can be defined using

[%%indexop
let get t x = Hashtbl.find t x 
let set t x y = Hashtbl.add t x y
] (*or let%indexop get ... and set ... *) 
let h =Hashtbl.create 10
let () = h.{"first"}<-1 (* h.{x}<-y  is equivalent to set h x y *) 
let x = h.{"first"} (* h.{x} is equivalent to get h x *)

Array-like (.()) and string-like operators (.[]) can be customized using [%%indexop.arraylike] and [%%indexop.stringlike] extension respectively. Bigarray multi-indices operators can be provided using get_2,get_3 or get_n identifiants and the equivalent variants for set. In signature, it is required to mark these special functions with an [@@indexop] attribute.

val get : ('a,'b) Hastbl -> 'a -> 'b [@@indexop]
val set : ('a,'b) Hastbl -> 'a -> 'b -> unit [@@indexop]

In vanilla ocaml mode, ppx_indexop has two majors limitations. First, index operator definitions mask the equivalent standard modules. In other words,

let%indexop.arraylike get a x = ...
and set a x y = ...

masks the Array module. Second, all the operators of a given type needs to be defined in the same extension node, e.g.

let%indexop get ...
let%indexop set ...

is invalid.