caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Re: [Caml-list] is there a way for turning strings to "format"s?
@ 2013-11-06 12:54 "Mark Adams"
  2013-11-07  7:33 ` Gabriel Scherer
  0 siblings, 1 reply; 10+ messages in thread
From: "Mark Adams" @ 2013-11-06 12:54 UTC (permalink / raw)
  To: caml-list

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
>
>
>

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Caml-list] is there a way for turning strings to "format"s?
  2013-11-06 12:54 [Caml-list] is there a way for turning strings to "format"s? "Mark Adams"
@ 2013-11-07  7:33 ` Gabriel Scherer
  2013-11-07  7:50   ` David MENTRE
  0 siblings, 1 reply; 10+ messages in thread
From: Gabriel Scherer @ 2013-11-07  7:33 UTC (permalink / raw)
  To: Mark Adams; +Cc: caml users

> 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

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Caml-list] is there a way for turning strings to "format"s?
  2013-11-07  7:33 ` Gabriel Scherer
@ 2013-11-07  7:50   ` David MENTRE
  2013-11-07 20:45     ` Ollie Frolovs
  0 siblings, 1 reply; 10+ messages in thread
From: David MENTRE @ 2013-11-07  7:50 UTC (permalink / raw)
  To: Gabriel Scherer; +Cc: Mark Adams, caml users

Hello,

2013/11/7 Gabriel Scherer <gabriel.scherer@gmail.com>:
> Do not, ever, use Obj.magic on formatting
> functions.

I would say: "Dot not, ever, use Obj.magic (FULL STOP)". ;-)

BTW, isn't "ksprintf " and related functions used when one wants to
play with format strings?

Best regards,
david

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Caml-list] is there a way for turning strings to "format"s?
  2013-11-07  7:50   ` David MENTRE
@ 2013-11-07 20:45     ` Ollie Frolovs
  2013-11-07 21:14       ` Yaron Minsky
  0 siblings, 1 reply; 10+ messages in thread
From: Ollie Frolovs @ 2013-11-07 20:45 UTC (permalink / raw)
  To: David MENTRE; +Cc: Gabriel Scherer, Mark Adams, caml users

When i first saw this question i had a deja vu moment and as was flicking through “Real World Ocaml” today i found this example in pages 173-174: 

#let fmt = : (‘a, ‘b, ‘c) format = “%i is an integer, %F is a float, \”%s\” is a string\n”;;
val fmt = (int -> float -> string -> ‘c, ‘b, ‘c) format = <abstr>

and then they say you can pass this format string to printf. I don’t know if they mean Jane Street’s Core printf or stock printf. I’m only a beginner myself.

— Ollie

On 7 Nov 2013, at 07:50, David MENTRE <dmentre@linux-france.org> wrote:

> Hello,
> 
> 2013/11/7 Gabriel Scherer <gabriel.scherer@gmail.com>:
>> Do not, ever, use Obj.magic on formatting
>> functions.
> 
> I would say: "Dot not, ever, use Obj.magic (FULL STOP)". ;-)
> 
> BTW, isn't "ksprintf " and related functions used when one wants to
> play with format strings?
> 
> Best regards,
> david
> 
> -- 
> 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


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Caml-list] is there a way for turning strings to "format"s?
  2013-11-07 20:45     ` Ollie Frolovs
@ 2013-11-07 21:14       ` Yaron Minsky
  0 siblings, 0 replies; 10+ messages in thread
From: Yaron Minsky @ 2013-11-07 21:14 UTC (permalink / raw)
  To: Ollie Frolovs; +Cc: David MENTRE, Gabriel Scherer, Mark Adams, caml users

It should work for both.

On Thu, Nov 7, 2013 at 3:45 PM, Ollie Frolovs <of12343@my.bristol.ac.uk> wrote:
> When i first saw this question i had a deja vu moment and as was flicking through “Real World Ocaml” today i found this example in pages 173-174:
>
> #let fmt = : (‘a, ‘b, ‘c) format = “%i is an integer, %F is a float, \”%s\” is a string\n”;;
> val fmt = (int -> float -> string -> ‘c, ‘b, ‘c) format = <abstr>
>
> and then they say you can pass this format string to printf. I don’t know if they mean Jane Street’s Core printf or stock printf. I’m only a beginner myself.
>
> — Ollie
>
> On 7 Nov 2013, at 07:50, David MENTRE <dmentre@linux-france.org> wrote:
>
>> Hello,
>>
>> 2013/11/7 Gabriel Scherer <gabriel.scherer@gmail.com>:
>>> Do not, ever, use Obj.magic on formatting
>>> functions.
>>
>> I would say: "Dot not, ever, use Obj.magic (FULL STOP)". ;-)
>>
>> BTW, isn't "ksprintf " and related functions used when one wants to
>> play with format strings?
>>
>> Best regards,
>> david
>>
>> --
>> 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

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Caml-list] is there a way for turning strings to "format"s?
  2013-11-06 12:57 ` Jeremy Yallop
@ 2013-11-06 13:08   ` Matej Kosik
  0 siblings, 0 replies; 10+ messages in thread
From: Matej Kosik @ 2013-11-06 13:08 UTC (permalink / raw)
  To: Jeremy Yallop; +Cc: OCaml

Guys,

Thanks for all the answers.

On 06/11/13 12:57, Jeremy Yallop wrote:
> On 6 November 2013 12:39, Matej Kosik
> <5764c029b688c1c0d24a2e97cd764f@gmail.com> wrote:
>> I would like to ask, how to do something like:
>>
>>         let computed_format = Printf.sprintf "%%0%dd" 5 in
>>         Printf.printf computed_format 42
> 
> For this example, you can use '*' in the format string to pass the
> width as an argument
> 
>          let computed_format = Printf.sprintf "%0*d" 5 in
>          computed_format 42
> 
> or even just
> 
>          Printf.sprintf "%0*d" 5 42
> 

This is great! \o/

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Caml-list] is there a way for turning strings to "format"s?
  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
  2 siblings, 1 reply; 10+ messages in thread
From: Jeremy Yallop @ 2013-11-06 12:57 UTC (permalink / raw)
  To: Matej Kosik; +Cc: OCaml

On 6 November 2013 12:39, Matej Kosik
<5764c029b688c1c0d24a2e97cd764f@gmail.com> wrote:
> I would like to ask, how to do something like:
>
>         let computed_format = Printf.sprintf "%%0%dd" 5 in
>         Printf.printf computed_format 42

For this example, you can use '*' in the format string to pass the
width as an argument

         let computed_format = Printf.sprintf "%0*d" 5 in
         computed_format 42

or even just

         Printf.sprintf "%0*d" 5 42

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Caml-list] is there a way for turning strings to "format"s?
  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
  2 siblings, 0 replies; 10+ messages in thread
From: Robert Jakob @ 2013-11-06 12:50 UTC (permalink / raw)
  To: caml-list; +Cc: Matej Kosik

> My question is: is it possible to turn strings to values that would
> be accepted by Printf.printf as its first parameter?

You might be able to use Scanf.format_from_string:
http://caml.inria.fr/pub/docs/manual-ocaml/libref/Scanf.html#VALformat_from_string

r.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Caml-list] is there a way for turning strings to "format"s?
  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
  2 siblings, 0 replies; 10+ messages in thread
From: Simon Cruanes @ 2013-11-06 12:44 UTC (permalink / raw)
  To: Matej Kosik; +Cc: OCaml

[-- 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 --]

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Caml-list] is there a way for turning strings to "format"s?
@ 2013-11-06 12:39 Matej Kosik
  2013-11-06 12:44 ` Simon Cruanes
                   ` (2 more replies)
  0 siblings, 3 replies; 10+ messages in thread
From: Matej Kosik @ 2013-11-06 12:39 UTC (permalink / raw)
  To: OCaml

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?

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2013-11-07 21:14 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-11-06 12:54 [Caml-list] is there a way for turning strings to "format"s? "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
  -- 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

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).