Git Product home page Git Product logo

ppx_indexop's Introduction

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.

ppx_indexop's People

Contributors

octachron avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.