I hadn't noticed that 4.06 will let me extend indexing. It's certainly a nice feature to have; I'll throw it in :)
I actually think I can use that to great effect - a lot of this is used to pack data into structures which are sent to/from C, the network, files, etc.. Having multiple ways of indexing it could be quite helpful for getting/setting multiple different types of data.
let ( .!{ } ) = get_1byte_int
let ( .@{ } ) = get_2byte_int
let ( .${ } ) = get_4byte_int
let ( .*{ } ) = get_8byte_int
Where the extension character is shift+number of bytes (at least on my keyboard...) I'm a bit worried that it'll start looking like Perl, but time will tell.
Thanks for the great ideas!

On Fri, Nov 17, 2017 at 5:01 AM, octachron <octa@polychoron.fr> wrote:
With OCaml 4.06, if what you really want is to have a nice syntax for array indexing, one solution would be to define your own indexing operators:

  let (  .%{ } ) a k = a.{k}
  let ( .%{ } <- ) a k x = a.{k} <- x

These operators can be used like standard indexing operators

    let x = a.%{ 0 } in
    a.%{ 1 } <- x

and exported with

   val (  .%{ } ): …
   val ( .%{ } <- ): …

Nevertheless, contrarily to Bigarray, there is no implicit conversion for multidimensional array,
in other words

    a .%{0,1}

will not work out of the box.

However, if needed, it is possible to use qualified indexing operators to circumvent this difficulty:

  module D1 = struct let ( .%{} ) a k = a.{k} end
  module D2 = struct let ( .%{} ) a (k,l) = a.{k,l} end
  let x_ij = mat.D2.%{i, j}
  let x_0 = vector.D1.%{0}

See also the manual for more details: http://caml.inria.fr/pub/docs/manual-ocaml/extn.html#s%3Aindex-operators .

− octachron.

--
Caml-list mailing list.  Subscription management and archives:
https://sympa.inria.fr/sympa/arc/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs



--
ç