caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: David Allsopp <dra-news@metastack.com>
To: "Андрей Бергман" <vkni@yandex.ru>,
	"caml-list@inria.fr" <caml-list@inria.fr>
Subject: RE: [Caml-list] type-safe printf
Date: Tue, 13 Sep 2016 15:38:53 +0000	[thread overview]
Message-ID: <E51C5B015DBD1348A1D85763337FB6D90135198FFF@Remus.metastack.local> (raw)
In-Reply-To: <337861473779549@web6h.yandex.ru>

Андрей Бергман wrote: 
> is there a good article or just short explanation how compile-type
> checking of Printf arguments works?
>
>
> Does it require special compiler support like in C++ compilers/analyzers,
> or one can somehow do the trick of parsing string literal using Ocaml
> language itself? (actually latter does not look possible to me)

It does require a certain amount of special support, yes - see the Pexp_constant(Pconst_string _) case in typing/typecore.ml, in particular the comment "Terrible hack for format strings"! In essence, a string is "magically" interpreted as a format string if it is used in a context where one would be expected (e.g. the usual Printf.printf). It's one of the reasons Pervasives.format_of_string exists (because they're type- rather than syntax-driven).

From that point onwards, it's a lot of GADTs, but doesn't require any special language support!

If you were adding Printf to OCaml for the first time now, you'd probably use quoted string literals instead (see http://caml.inria.fr/pub/docs/manual-ocaml/extn.html#sec249) - and the entire thing would then be in OCaml with no special case hacks.


David


  parent reply	other threads:[~2016-09-13 15:38 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-09-13 15:12 Андрей Бергман
2016-09-13 15:25 ` Mario Pereira
2016-09-13 15:36   ` Maxime Dénès
2016-09-13 15:31 ` Kakadu
2016-09-13 15:40   ` Daniel Bünzli
2016-09-14 11:19   ` Oleg
2016-09-13 15:38 ` David Allsopp [this message]
2016-09-13 17:54 ` Андрей Бергман

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=E51C5B015DBD1348A1D85763337FB6D90135198FFF@Remus.metastack.local \
    --to=dra-news@metastack.com \
    --cc=caml-list@inria.fr \
    --cc=vkni@yandex.ru \
    /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).