> Thanks Gabriel, very nice solution. If I go this way, I guess there is > no way to access array elements using the usual a.(i) syntax (where i > = M.key i)... [...] > Is this a problem I can solve using a camlp4 decorator ?
I don't think you need -- nor want to use -- a camlp4 extension. a.(i) is desugared into (Array.get a i) at a purely syntactical level in OCaml, so you could overload its behavior by changing the Array module in the typing environment. With my example you could write, for example: module A1 = ArrayMake(struct end) let () = let module Array = A1 in let k = A1.key in assert (A1.make 3 true).(k 2);; You could even define the ArrayMake functor so that it returns a structure with an Array submodule. You would then write, using 3.12 "local open" syntax: module A1 = ArrayMake(struct end) let () = let open A1 in assert (Array.make 3 true).(k 2) That said, I don't think that the slight readability benefit of writing a.(i) instead of (get a i) will outweigh the confusion among your readers that don't understand what you're doing with this weird Array stuff. On Thu, Mar 15, 2012 at 12:04 PM, Pietro Abate <pietro.ab...@pps.jussieu.fr> wrote: > On 15/03/12 00:00, Gabriel Scherer wrote: >> Here is a proposal: >> >> https://gitorious.org/gasche-snippets/private-array-keys-type/blobs/master/private_array_key_types.ml >> >> It works by using a functor to generate "fresh" private types for >> keys. Note that the arrays themselves are still polymorphic (no >> IntArray FloatArray etc.). The user still has to use the discipline to >> produce a new application of ArrayMake each time she wants to use a >> different kind of array: if she only does `module A = ArrayMake(struct >> end)` and then use `A` for everything, there will be no additional >> safety guarantee. > > Thanks Gabriel, very nice solution. If I go this way, I guess there is > no way to access array elements using the usual a.(i) syntax (where i > = M.key i)... (I've noticed your cleaver use of private on the array > type to avoid using the normal array syntax on your private arrays). > > Is this a problem I can solve using a camlp4 decorator ? > > This seems a bit complicated as the a.(i) syntax will be context > dependent, that is, the same syntax used with two different array types > should be translated to different get/set calls... and at the camlp4 > level I don't have access to type information... Ideally, instead of > changing all my array access calls, I'd like just to change the type > of my indexes such that all my generic ints will be replaced by > M.key of the appropriate type... > > p > > ps: please do not Cc me. I'm subscribed to the list. > > -- > Caml-list mailing list. Subscription management and archives: > https://sympa-roc.inria.fr/wws/info/caml-list > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners > Bug reports: http://caml.inria.fr/bin/caml-bugs > -- Caml-list mailing list. Subscription management and archives: https://sympa-roc.inria.fr/wws/info/caml-list Beginner's list: http://groups.yahoo.com/group/ocaml_beginners Bug reports: http://caml.inria.fr/bin/caml-bugs