caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Simon Cruanes <simon.cruanes.2007@m4x.org>
To: Matej Kosik <5764c029b688c1c0d24a2e97cd764f@gmail.com>
Cc: OCaml <caml-list@inria.fr>
Subject: Re: [Caml-list] is there a way for turning strings to "format"s?
Date: Wed, 6 Nov 2013 13:44:08 +0100	[thread overview]
Message-ID: <20131106124408.GQ1501@emmental.inria.fr> (raw)
In-Reply-To: <527A389B.9070408@gmail.com>

[-- Attachment #1: Type: text/plain, Size: 861 bytes --]

Le Wed, 06 Nov 2013, Matej Kosik a écrit :

> Hi,
> 
> I would like to ask, how to do something like:
> 
> 	let computed_format = Printf.sprintf "%%0%dd" 5 in
> 	Printf.printf computed_format 42
> 
> The above code is rejected, because "computed_format" is of type "string" whereas
> 
>         ('a -> 'b, out_channel, unit) format =
>            ('a -> 'b, out_channel, unit, unit, unit, unit) format6
> 
> type was expected.
> 
> My question is: is it possible to turn strings to values that would be accepted by Printf.printf as its first parameter?

I don't think you can in general, because format strings are typed (so
that the compiler can check the correctness of Printf.printf calls).

However, you could try something along

let computed_format = Printf.sprintf "%%0%d%d" 5 in
print_string (computed_format 42)

-- 
Simon

[-- Attachment #2: Type: application/pgp-signature, Size: 836 bytes --]

  reply	other threads:[~2013-11-06 12:44 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-11-06 12:39 Matej Kosik
2013-11-06 12:44 ` Simon Cruanes [this message]
2013-11-06 12:50 ` Robert Jakob
2013-11-06 12:57 ` Jeremy Yallop
2013-11-06 13:08   ` Matej Kosik
2013-11-06 12:54 "Mark Adams"
2013-11-07  7:33 ` Gabriel Scherer
2013-11-07  7:50   ` David MENTRE
2013-11-07 20:45     ` Ollie Frolovs
2013-11-07 21:14       ` Yaron Minsky

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=20131106124408.GQ1501@emmental.inria.fr \
    --to=simon.cruanes.2007@m4x.org \
    --cc=5764c029b688c1c0d24a2e97cd764f@gmail.com \
    --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;
as well as URLs for NNTP newsgroup(s).