caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Damien Doligez <damien.doligez@inria.fr>
To: "David J. Trombley" <swampy@spacebird.net>
Cc: "<caml-list@inria.fr>" <caml-list@inria.fr>
Subject: Re: [Caml-list] format4 query
Date: Wed, 19 May 2004 19:20:43 +0200	[thread overview]
Message-ID: <DBEEB198-A9B8-11D8-A6E5-00039310CAE8@inria.fr> (raw)
In-Reply-To: <006f01c43dbc$4642deb0$10015c0a@word>

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


      reply	other threads:[~2004-05-19 17:20 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-05-19  6:15 David J. Trombley
2004-05-19  7:22 ` Olivier Andrieu
2004-05-19 16:13   ` David J. Trombley
2004-05-19 17:20     ` Damien Doligez [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=DBEEB198-A9B8-11D8-A6E5-00039310CAE8@inria.fr \
    --to=damien.doligez@inria.fr \
    --cc=caml-list@inria.fr \
    --cc=swampy@spacebird.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).