caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Re: [Caml-list] Printf.sprintf ?
       [not found] <200108151045.f7FAjl423204@sarg.ryerson.ca>
@ 2001-08-15 11:04 ` Johann Spies
  0 siblings, 0 replies; 4+ messages in thread
From: Johann Spies @ 2001-08-15 11:04 UTC (permalink / raw)
  To: ocaml mailing list; +Cc: Dave Mason

Dave Mason <dmason@sarg.ryerson.ca> writes:

> But unless you force it to unify as a format, it is treated as a
> string, so you get the effect you saw.

Thanks Dave.  And how do I do that (force it to unify as a format)?

I want to use this to build a sql-query recursively from a list.


Johann
-- 
Johann Spies          Telefoon: 021-808 4036
Informasietegnologie, Universiteit van Stellenbosch

     "Praying always with all prayer and supplication in the
      Spirit, and watching thereunto with all perseverance 
      and supplication for all saints."       Ephesians 6:18
-------------------
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


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

* Re: [Caml-list] Printf.sprintf ?
  2001-08-15 15:27 ` Brian Rogoff
@ 2001-08-16  9:27   ` Remi VANICAT
  0 siblings, 0 replies; 4+ messages in thread
From: Remi VANICAT @ 2001-08-16  9:27 UTC (permalink / raw)
  To: caml-list

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


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

* Re: [Caml-list] Printf.sprintf ?
  2001-08-15  9:49 Johann Spies
@ 2001-08-15 15:27 ` Brian Rogoff
  2001-08-16  9:27   ` Remi VANICAT
  0 siblings, 1 reply; 4+ messages in thread
From: Brian Rogoff @ 2001-08-15 15:27 UTC (permalink / raw)
  To: Johann Spies; +Cc: ocaml mailing list

On 15 Aug 2001, Johann Spies wrote:

> Can somebody please explain this to me:
> 
> This works as expected:
> 
> # let s = Printf.sprintf "%s" "enigiets";;
> val s : string = "enigiets"
> 
> But this not:
> 
> # let q = "%s";;
> val q : string = "%s"
> # let s = Printf.sprintf q "enigiets";;
> Characters 23-24:
> This expression has type string but is here used with type
>   (string -> 'a, unit, string) format
> 
> Why?

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

-- Brian

(*) Yes, I'm a C programmer too so it's a welcome hack. 

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


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

* [Caml-list] Printf.sprintf ?
@ 2001-08-15  9:49 Johann Spies
  2001-08-15 15:27 ` Brian Rogoff
  0 siblings, 1 reply; 4+ messages in thread
From: Johann Spies @ 2001-08-15  9:49 UTC (permalink / raw)
  To: ocaml mailing list

Can somebody please explain this to me:

This works as expected:

# let s = Printf.sprintf "%s" "enigiets";;
val s : string = "enigiets"

But this not:

# let q = "%s";;
val q : string = "%s"
# let s = Printf.sprintf q "enigiets";;
Characters 23-24:
This expression has type string but is here used with type
  (string -> 'a, unit, string) format

Why?

Johann
-- 
Johann Spies          Telefoon: 021-808 4036
Informasietegnologie, Universiteit van Stellenbosch

     "Praying always with all prayer and supplication in the
      Spirit, and watching thereunto with all perseverance 
      and supplication for all saints."       Ephesians 6:18
-------------------
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


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

end of thread, other threads:[~2001-08-16 10:06 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <200108151045.f7FAjl423204@sarg.ryerson.ca>
2001-08-15 11:04 ` [Caml-list] Printf.sprintf ? Johann Spies
2001-08-15  9:49 Johann Spies
2001-08-15 15:27 ` Brian Rogoff
2001-08-16  9:27   ` Remi VANICAT

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