* mixing infix functions and the format4 types
@ 2009-03-30 0:18 Anil Madhavapeddy
2009-03-30 9:10 ` Zheng Li
0 siblings, 1 reply; 3+ messages in thread
From: Anil Madhavapeddy @ 2009-03-30 0:18 UTC (permalink / raw)
To: OCaml
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 there a trick to let this work, or do I need to stick to using non-
infix functions for formatting?
thanks
Anil
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: mixing infix functions and the format4 types
2009-03-30 0:18 mixing infix functions and the format4 types Anil Madhavapeddy
@ 2009-03-30 9:10 ` Zheng Li
2009-03-30 10:45 ` [Caml-list] " Anil Madhavapeddy
0 siblings, 1 reply; 3+ messages in thread
From: Zheng Li @ 2009-03-30 9:10 UTC (permalink / raw)
To: Anil Madhavapeddy; +Cc: OCaml
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
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2009-03-30 10:45 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-03-30 0:18 mixing infix functions and the format4 types Anil Madhavapeddy
2009-03-30 9:10 ` Zheng Li
2009-03-30 10:45 ` [Caml-list] " Anil Madhavapeddy
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox