Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Zheng Li <zheng_li@users.sourceforge.net>
To: Anil Madhavapeddy <anil@recoil.org>
Cc: OCaml <caml-list@inria.fr>
Subject: Re: mixing infix functions and the format4 types
Date: Mon, 30 Mar 2009 11:10:54 +0200	[thread overview]
Message-ID: <49D08C9E.4060707@users.sourceforge.net> (raw)
In-Reply-To: <A3CFE8B7-5445-4AE0-828E-167A7586AEAC@recoil.org>

Hello,

On 3/30/2009 2:18 AM, Anil Madhavapeddy wrote:
> Is there any way to mix infix operators with the Printf format6 types?
> I have a code printing module where this would be quite convenient
> syntactically, but the obvious attempt didn't work:
>
> $ ocaml
> Objective Caml version 3.11.0
> # open Printf;;
> # let myfn m s = let xfn o = m ^ ": " ^ o in kprintf xfn s;;
> val myfn : string -> ('a, unit, string, string) format4 -> 'a = <fun>
> # myfn "foo" "bar";;
> - : string = "foo: bar"
> # myfn "foo" "%s %s" "bar1" "bar2";;
> - : string = "foo: bar1 bar2"
> # let (---) m s = myfn m s;;
> val ( --- ) : string -> ('a, unit, string, string) format4 -> 'a = <fun>
> # "foo" --- "bar";;;
> - : string = "foo: bar"
> # "foo" --- "%s %s" "bar1" "bar2";;
> Error: This expression is not a function, it cannot be applied
>

Is extra separators acceptable?

# let ($) f x = f x;;
val ( $ ) : ('a -> 'b) -> 'a -> 'b = <fun>
# "foo" --- "%s %s %s" $ "bar1" $ "bar2" $ "bar3";;
- : string = "foo: bar1 bar2 bar3"

and, maybe in another way

# let (%) fmt x = Printf.sprintf fmt x;;
val ( % ) : ('a -> 'b, unit, string) format -> 'a -> 'b = <fun>
# "%s %s %s" % "bar1" $ "bar2" $ "bar3";;
- : string = "bar1 bar2 bar3"

HTH.
--
Zheng



  reply	other threads:[~2009-03-30  9:09 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-03-30  0:18 Anil Madhavapeddy
2009-03-30  9:10 ` Zheng Li [this message]
2009-03-30 10:45   ` [Caml-list] " Anil Madhavapeddy

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=49D08C9E.4060707@users.sourceforge.net \
    --to=zheng_li@users.sourceforge.net \
    --cc=anil@recoil.org \
    --cc=caml-list@inria.fr \
    /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