Le 08/12/2011 03:33, Edgar Friendly a écrit :
I'm interested in having compile-time reflection like this, but maybe
with a bit more generality. Maybe an identifier can be built out of the
type in a generic way...

external show : 'a -> string = "%type_to_string"
external print : out_channel -> 'a -> unit
external cmp : 'a -> 'a -> int = "%type_compare"

When does this function return something different than 0?

Jérémie Dimino a écrit :
 If we want to really use it we can do something more generic
than just generating printers. For example we could generate type
representation values from which we would generate printers,
marshallers, ...

I'm not sure that we can use a function like :
external type_expr : 'a -> 'a type_expr (with GADT)
(or just external type_expr : 'a -> type_expr)

in order to write a generic printer, since during the definition the value is polymorphic. We only know the type at the call site. Of course the user can call the function himself :

val gen_to_string : 'a type_expr -> 'a -> string

let v = ... in
(** debug *)
print_string (gen_to_string (type_expr v) v)


Perhaps we can ask that the first argument is implicitly given by the OCaml compiler (extending the "type a" notation) :

let gen_to_string type (a:repr) (x:a) =
        ...  (repr : a type_expr) ...

....

let v = ... in
(** debug *)
print_string (gen_to_string v)


In fact the LexiFi extension to OCaml called implicit argument, can do something similar :
 http://www.lexifi.com/blog/implicit-values
(You can look for "Combined with GADTs, one could do something like (not tested!):")

But with all these solutions we will never be able to write a generic printer which can print values that come from a dynamic plugin (um.. wait and with first order modules?...)


--
François Bobot

--
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

Reply via email to