Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

compilation incorrecte de Printf.sprintf #3116

Closed
vicuna opened this issue Jan 2, 2002 · 0 comments
Closed

compilation incorrecte de Printf.sprintf #3116

vicuna opened this issue Jan 2, 2002 · 0 comments
Labels

Comments

@vicuna
Copy link

vicuna commented Jan 2, 2002

Original bug ID: 769
Reporter: administrator
Status: closed
Resolution: not a bug
Priority: normal
Severity: minor
Category: ~DO NOT USE (was: OCaml general)

Bug description

(réponse au bug report 765 de David Monniaux, suivi d'une suggestion)

Salut David,

le comportement est surprenant, mais ce n'est pas vraiment un bug.
En fait, les fonctions *printf ne devraient pas être partiellement
appliquée, parce que la sémantique n'est pas spécifiée dans ce cas.

En particulier, sprintf opère de la manière suivante: on commence par
allouer un Buffer.t et on parse la chaine "format"; dès que l'on tombe
sur un %..., on suspend en attendant l'argument correspondant; quand on
arrive à la fin de la chaine, on renvoie le contenu du Buffer.
Il se trouve que dans la version courante, le buffer est également
vidé quand on extrait son contenu, et donc ça donne:

let f = Printf.sprintf "Bla %s";;

val f : string -> string =

f "A";;

  • : string = "Bla A" (et le buffer interne est vide)

f "B";;

  • : string = "B"

Dans les vieilles versions d'OCaml (par exemple avec OCaml 2.02), le
buffer n'était pas vidé et on obtenait "Bla AB", ce qui n'est pas mieux !

Remarque que même avec Printf.printf, il faut se méfier:

let f = Printf.printf "Bla %s\n";;

Bla val f : string -> unit =

f "A";;

A

  • : unit = ()

f "B";;

B

  • : unit = ()

Bref, SUGGESTION (d'où le Cc: caml-bugs): ne serait-ce pas une bonne
idée d'empecher statiquement les applications partielles pour les
fonctions à la printf (il y a de toute manière une règle de typage
ad hoc) ?

--
Alain

@vicuna vicuna closed this as completed Jan 4, 2002
@vicuna vicuna added the bug label Mar 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant