Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Printf.sprintf ?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Remi VANICAT <vanicat@l...>
Subject: Re: [Caml-list] Printf.sprintf ?
Brian Rogoff <bpr@best.com> writes:

> Because Printf is a bit of a hack, in that the syntax for a format string
> is the same as the syntax for a regular string. As a beginner, when you
> use Printf, it's easier to use the format string as a literal and not
> as a variable. If you feel you must do this, the polite way is as follows 
> 
> # let q = ("%s" : ('a, 'b, 'c) format);;  
> val q : (string -> 'a, 'b, 'a) format = <abstr>
> # let s = Printf.sprintf q "enigiets";;
> val s : string = "enigiets"
> 
> and there's also a rude way (forgive me Berke! :) 
> 
>   let s : string = Printf.sprintf (Obj.magic q) "enigiets";;

it's often not a good idea : 

# let q = "%s";;
val q : string = "%s"
# let s : string = Printf.sprintf (Obj.magic q)0;;        
Segmentation fault
moi@debian:~$

this break the type system.

better use the first solution.
-- 
Rémi Vanicat
vanicat@labri.u-bordeaux.fr
http://dept-info.labri.u-bordeaux.fr/~vanicat
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr