caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jiten Pathy <jpathy@fssrv.net>
To: David Allsopp <dra-news@metastack.com>
Cc: "caml-list@inria.fr" <caml-list@inria.fr>
Subject: Re: [Caml-list] format strings
Date: Mon, 9 Feb 2015 18:47:19 -0800	[thread overview]
Message-ID: <CAL2Z3DBVtOVU+7NUeEUG-jHvFGi15Uw4VPVAL=1Hz82b1MSGOQ@mail.gmail.com> (raw)
In-Reply-To: <CAL2Z3DAU2wFc5OmtQvXXL15iWRoZRmd4DUPtKrJc-tDAwY=wiQ@mail.gmail.com>

nvm. make_printf is what I am after. Some complicated stuff is going
on in CamlInternalFormat.

On Mon, Feb 9, 2015 at 5:05 PM, Jiten Pathy <jpathy@fssrv.net> wrote:
> Is there a way I can define a function which has the same signature of
> printf family but is a no-op?
>
> f : ('a, 'b, unit) format -> 'a
>
> assert(f "%s %d" "hello" 1 = ());;
>
> On Mon, Feb 9, 2015 at 9:32 AM, David Allsopp <dra-news@metastack.com> wrote:
>> Jiten Pathy wrote:
>>> When i define a wrapper around format functions like
>>>
>>> let f s = (fun x -> Printf.sprintf s x);;
>>>
>>> val f : ('a -> 'b, unit, bytes) format -> 'a -> 'b = <fun>
>>>
>>> I am confused about the inferred type. where does this 'b comes from?
>>
>> Your wrapper carries the requirement that the format [s] must have at least one %-parameter. Writing without the fun might make this more obvious:
>>
>> let f s x = Printf.sprintf s x
>>
>> The 'a -> 'b is the encoding of this requirement. f "foo" will type. If instead you wrote:
>>
>> let f s = Printf.sprintf s
>>
>> you'd get the type you're expecting: ('a, unit, bytes) format -> 'a
>>
>> and you can have format strings with no %-parameters (so f "foo" now types). If you meant to have it so that the format string must have *exactly* one %-parameter then you need an annotation:
>>
>> let f s x : string = Printf.sprintf s x
>>
>> and now f "%d" will type, but f "%s %d" will not.
>>
>> HTH,
>>
>>
>> David

  reply	other threads:[~2015-02-10  2:47 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-02-09 11:58 Jiten Pathy
2015-02-09 12:25 ` John F Carr
2015-02-09 16:28   ` Jiten Pathy
2015-02-09 17:32 ` David Allsopp
2015-02-10  1:05   ` Jiten Pathy
2015-02-10  2:47     ` Jiten Pathy [this message]
2015-02-10  7:44       ` David Allsopp
2015-02-10  7:55         ` Jiten Pathy
2015-02-10  8:25           ` David Allsopp
2015-02-10  8:34             ` Jiten Pathy

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='CAL2Z3DBVtOVU+7NUeEUG-jHvFGi15Uw4VPVAL=1Hz82b1MSGOQ@mail.gmail.com' \
    --to=jpathy@fssrv.net \
    --cc=caml-list@inria.fr \
    --cc=dra-news@metastack.com \
    /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).