Browse thread
[Caml-list] kprintf with user formatters
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | Pierre Weis <pierre.weis@i...> |
| Subject: | Re: [Caml-list] kprintf with user formatters |
[...]
> I had the same problem a month ago,
> I ended with the following ugly function:
>
> let log b fmt =
> if b then Printf.printf fmt
> else
> let rec f x = Obj.magic f in
> f fmt
> (* val f: bool -> ('a, out_channel, unit) format -> 'a *)
>
> (I think this is safe since f just eats its arguments,
> and gets typed with "-rectypes" : ('b->'a) as 'a)
No, this is not safe: you break the type system and create buggy
values like that.
# let log b fmt =
if b then Printf.printf fmt else
let rec f x = Obj.magic f in f fmt;;
val log : bool -> ('a, out_channel, unit) format -> 'a = <fun>
# log false "";;
- : unit = <unknown constructor>
# let x = log false "";;
val x : unit = <unknown constructor>
# Marshal.to_string;;
- : 'a -> Marshal.extern_flags list -> string = <fun>
# Marshal.to_string x [];;
Exception: Invalid_argument "output_value: abstract value (outside
heap)".
There is clearly something wrong!
> maybe a function "zprintf" could be added to Printf and Format to do
> this job ?
It is overkill to introduce a new printf function just for that case.
I suggest we try a bit harder to find an acceptable solution without
introducing zprintf :)
Best regards,
Pierre Weis
INRIA, Projet Cristal, Pierre.Weis@inria.fr, http://pauillac.inria.fr/~weis/
-------------------
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