caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Gabriel Scherer <gabriel.scherer@gmail.com>
To: Mark Adams <mark@proof-technologies.com>
Cc: caml users <caml-list@inria.fr>
Subject: Re: [Caml-list] is there a way for turning strings to "format"s?
Date: Thu, 7 Nov 2013 08:33:49 +0100	[thread overview]
Message-ID: <CAPFanBE0s4Qy+5yvOvY4Y3crt92Qc_6Wf0yVQ8QDx36TR1e8Hw@mail.gmail.com> (raw)
In-Reply-To: <1383742480351@names.co.uk>

> There's always the nasty 'Obj.magic' for conversion between arbitrary types:
>
>     let computed_format = Printf.sprintf "%%0%dd" 5 in
>     Printf.printf (Obj.magic computed_format) 42

This is terribly wrong. Not only does it remove any form of static
typing on today's version of OCaml, it is also assured to segfault as
soon as the format representation changes (which may happen in a close
future). Do not use Obj.magic.

Matej: Deciding the number of decimal points or 0-padding dynamically
is one of those eccentric features that are in fact already part of
format.

  # Printf.printf "%0*d" 6 2;;
  000002- : unit = ()

Format will be your friend as long as you stay inside the things they
directly support -- look at the documentation carefully to discover
some surprisingly expressive features. If you ever step outside this
zone, the temptation is great to hack your way back in, but that will
only lead to trouble (consider simply using OCaml code to implement
your printing logic then). Do not, ever, use Obj.magic on formatting
functions.

On Wed, Nov 6, 2013 at 1:54 PM, "Mark Adams"
<mark@proof-technologies.com> wrote:
> There's always the nasty 'Obj.magic' for conversion between arbitrary types:
>
>     let computed_format = Printf.sprintf "%%0%dd" 5 in
>     Printf.printf (Obj.magic computed_format) 42
>
> Surely there must be a nicer way of doing this, but I don't know of one.
>
> Mark.
>
> on 6/11/13 12:40 PM, Matej Kosik <5764c029b688c1c0d24a2e97cd764f@gmail.com>
> wrote:
>
>> 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?
>>
>> --
>> Caml-list mailing list.  Subscription management and archives:
>> https://sympa.inria.fr/sympa/arc/caml-list
>> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
>> Bug reports: http://caml.inria.fr/bin/caml-bugs
>>
>>
>>
>
> --
> Caml-list mailing list.  Subscription management and archives:
> https://sympa.inria.fr/sympa/arc/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs

  reply	other threads:[~2013-11-07  7:34 UTC|newest]

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

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=CAPFanBE0s4Qy+5yvOvY4Y3crt92Qc_6Wf0yVQ8QDx36TR1e8Hw@mail.gmail.com \
    --to=gabriel.scherer@gmail.com \
    --cc=caml-list@inria.fr \
    --cc=mark@proof-technologies.com \
    /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).