Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Does this function exist?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Chris Hecker <checker@d...>
Subject: Re: [Caml-list] Does this function exist?

>I can use printf, but it isn't very easy to write when you have i.e.
>list of pairs of lists. And i believe that there is unversal ``print''.
>Can you help?

This has gone around before, and I think it's impossible because there's no 
type information available at runtime.

However, it seems like it would solve 90% of the problems if we could 
generate the code for such a function for a specific type using a tool, 
like camlp4 or something, and then compile that in when we want it.  In 
other words, say I have this:

type foo = A of int | B of int list

Then have camlp4 generate

let print_foo fmt f =
   Format.fprintf fmt "@[foo = ";
   begin match f with
     A i -> Format.fprintf fmt "A %d" i
   | B il -> Format.fprintf fmt "B [%s]"
         (List.fold_left ~f:(fun s i -> s ^ Printf.sprintf "%s%d" (if s <> 
"" then "; " else "") i) ~init:"" il)
   end;
   Format.fprintf fmt "@]"

or whatever (you'd probably want it to just return a string so you could 
compose them, but then you'd want the formatting stuff in there, so it's 
unclear exactly which would be better...maybe Format.sprintf works).  The 
idea is you don't want to have to write that code by hand, but generating 
it would be fine.

Is this something camlp4 can do?

Chris


-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners