English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
Re: [Caml-list] format4 query (Modified by Damien Doligez)
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2004-05-19 (17:23)
Subject: Re: [Caml-list] format4 query (Modified by Damien Doligez)
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
     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