caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Gabriel Scherer <gabriel.scherer@gmail.com>
To: Matej Kosik <5764c029b688c1c0d24a2e97cd764f@gmail.com>
Cc: "caml-list@inria.fr" <caml-list@inria.fr>
Subject: Re: [Caml-list] [batteries] ... how to create (format) directives that do not take any arguments?
Date: Mon, 25 Nov 2013 17:27:17 +0100	[thread overview]
Message-ID: <CAPFanBGc_1gKtuzBGJCfuJzf8D=HbiOHW7y-mQpFjoPtNSJ4uQ@mail.gmail.com> (raw)
In-Reply-To: <529370C0.9020801@gmail.com>

Adding custom printing directives requires a syntax extension to
rewrite those directives into OCaml code. The built-in support for
magicly-typed formats in the OCaml language is not extensible (it is
complex enough as it is).
Batteries 1 used to provide such a syntax extension, but we
(re)discovered that users don't like syntax extensions: they make code
harder to compile/deploy, are controversial, and in the long run often
make our life harder instead of easier. Batteries 2 does not come with
any syntax extension, and I think we're better off as is (of course
you're free to add your own in your code).

Of course, such a natural idea is bound to be reused, and one of the
first libraries released by Jérémie Dimino at Jane Street was
precisely such a custom-printf camlp4 extension:
  https://github.com/janestreet/custom_printf

Feel free to reuse it or extend it for any project where it makes
sense. In particular, I don't think that using it implies any
dependency of your preprocessed code on Core, though of course you'll
need Sexplib to use the format-to-sexplib feature. My purely personal
advice would be to seriously explore non-syntax-extension approaches
(combinator library?) before making such a step.

On Mon, Nov 25, 2013 at 4:46 PM, Matej Kosik
<5764c029b688c1c0d24a2e97cd764f@gmail.com> wrote:
> Dear all,
>
> I would like to define custom directives, that would enable me to write code like e.g. this:
>
> Print.printf "regular %bold_on bold %bold_off regular %italic_on italic %italic_off";
>
> This might expand to (in HTML codes)
>
>               regular <B>bold</B> regular <I>italic</I>
>
> or in ANSI codes to something analogous.
>
> I am currently looking at:
>
>   http://ocaml-batteries-team.github.io/batteries-included/hdoc/BatPrint.html
>
> It refers to:
>
>   https://github.com/ocaml-batteries-team/batteries-included/blob/master/examples/snippets/test_printf.ml
>
> Those things work, although there is no example for:
>
>   Print.literal
>
> which might be (?) what I need to employ.
>
> Can somebody give me some advice how to create simple "parameterless format directives" (like those above)?
>
> ----
>
> Basically, I just want to refactor some weird markup out of the literal string while I do not want to reinsert the refactored stuff via %s because it is not maintainable.
>
> --
> 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

  reply	other threads:[~2013-11-25 16:28 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-11-25 15:46 Matej Kosik
2013-11-25 16:27 ` Gabriel Scherer [this message]
2013-11-27 11:37   ` Matej Kosik
2013-11-28  6:30     ` [Caml-list] [batteries] ... how to create (format) directives oleg
2013-11-29  9:32       ` Matej Kosik
2013-11-30  3:15         ` oleg
2013-11-27 11:54 ` [Caml-list] [batteries] ... how to create (format) directives that do not take any arguments? Jeremie Dimino
2013-11-27 12:52   ` Matej Kosik
2013-11-27 13:00     ` Jeremie Dimino
2013-11-29  9:32       ` Matej Kosik

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='CAPFanBGc_1gKtuzBGJCfuJzf8D=HbiOHW7y-mQpFjoPtNSJ4uQ@mail.gmail.com' \
    --to=gabriel.scherer@gmail.com \
    --cc=5764c029b688c1c0d24a2e97cd764f@gmail.com \
    --cc=caml-list@inria.fr \
    /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).