Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Virgile Prevosto <virgile.prevosto@m4x.org>
To: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] class and printf
Date: Mon, 30 Oct 2006 14:28:01 +0100	[thread overview]
Message-ID: <20061030142801.61a936c1@localhost> (raw)
In-Reply-To: <cd67f63a0610300447r23b1c61cub33a538edc33b53e@mail.gmail.com>

Hello,
Le lun 30 oct 2006 14:47:06 CET,
"Nicolas Pouillard" <nicolas.pouillard@inria.fr> a écrit :

> On 10/30/06, Anastasia Gornostaeva <ermine@ermine.pp.ru> wrote:
> > Hello.
> >
> > What I need to drink to compile it?
> >
> > class foo ch =
> > object
> >  method myprintf = Printf.fprintf ch
> > end
> This one works:
> 
> class ['a] foo ch =
> object
>  method myprintf (fmt : ('a, out_channel, unit) format) =
>    Printf.fprintf ch fmt
> end
> 

While this solution compiles fine, there might an issue with it,
depending on the context where it is to be used: an instance of foo
won't be really polymorphic, so that you'll have to use it with 
the same kind of format string everywhere (from another point of
view, it means that you must define an instance for "%s", one for "%d",
etc.):

# let  bar = new foo stdout;;
val bar : '_a foo = <obj>
# bar#myprintf "foo\n" (* fixes the kind of format string bar
                            can handle *);;
* foo
- : unit = ()
# bar#myprintf "%s" "foo";;
Characters 0-12:
  bar#myprintf "%s" "foo";;
  ^^^^^^^^^^^^
This function is applied to too many arguments,
maybe you forgot a `;'
# bar;; (* check that '_a has been unified with unit *)
- : unit foo = <obj>

Maybe a polymorphic method would be better here:

class foo ch =
object
 method myprintf : 'a.('a, out_channel, unit) format -> 'a =
   fun fmt -> Printf.fprintf ch fmt
end
;;
            class foo :
  out_channel ->
  object method myprintf : ('a, out_channel, unit) format -> 'a end
# let bar = new foo stdout;;

val bar : foo = <obj>
#   bar#myprintf "foo\n";;

foo
- : unit = ()
#   bar#myprintf "%s" "foo";;

foo- : unit = ()


-- 
E tutto per oggi, a la prossima volta.
Virgile


  reply	other threads:[~2006-10-30 13:28 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-10-30 12:42 Anastasia Gornostaeva
2006-10-30 12:47 ` [Caml-list] " Nicolas Pouillard
2006-10-30 13:28   ` Virgile Prevosto [this message]
2006-10-30 12:49 ` Jon Harrop
2006-10-30 13:21 ` Jacques Garrigue

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=20061030142801.61a936c1@localhost \
    --to=virgile.prevosto@m4x.org \
    --cc=caml-list@yquem.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