Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] format4 query
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Damien Doligez <damien.doligez@i...>
Subject: Re: [Caml-list] format4 query
On May 19, 2004, at 18:13, David J. Trombley wrote:

> Right, as I said I can get simple examples to work.   But I
> can't get syntax such as
>
>  # let g (l : listener) (k : int) fmt =
>     match k with
>       0 -> 0
>     | x -> Printf.kprintf l fmt; k;;
>
> val g : listener -> int -> ('a, unit, string, unit)
> format4 -> int = <fun>
>
> to function, as you can see the format type has been
> inferred to a type that is not useful at all.
>
> I'd like to use kprintf as a side effect to a function which
> evaluates to some type, but I would not like that type or
> the unit type of a ; evaluation to affect the format type.

Here is the trick: the call to kprintf must always be the last
thing that your function does.  You get control back when kprintf
calls your continuation function:

   let g (l : listener) (k : int) fmt =
     let cont s =
       match k with
       | 0 -> 0
       | _ -> l s; k
     in
     Printf.kprintf cont fmt
   ;;

Note that kprintf will be called in all cases, but that's
unavoidable because it has to consume the arguments that will
be passed to g after the fmt argument.

-- Damien

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