* class and printf
@ 2006-10-30 12:42 Anastasia Gornostaeva
2006-10-30 12:47 ` [Caml-list] " Nicolas Pouillard
` (2 more replies)
0 siblings, 3 replies; 5+ messages in thread
From: Anastasia Gornostaeva @ 2006-10-30 12:42 UTC (permalink / raw)
To: caml-list
Hello.
What I need to drink to compile it?
class foo ch =
object
method myprintf = Printf.fprintf ch
end
The compiler says
The method myprintf has type ('a, out_channel, unit) format -> 'a where 'a
is unbound
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] class and printf
2006-10-30 12:42 class and printf Anastasia Gornostaeva
@ 2006-10-30 12:47 ` Nicolas Pouillard
2006-10-30 13:28 ` Virgile Prevosto
2006-10-30 12:49 ` Jon Harrop
2006-10-30 13:21 ` Jacques Garrigue
2 siblings, 1 reply; 5+ messages in thread
From: Nicolas Pouillard @ 2006-10-30 12:47 UTC (permalink / raw)
To: Anastasia Gornostaeva; +Cc: caml-list
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
>
> The compiler says
> The method myprintf has type ('a, out_channel, unit) format -> 'a where 'a
> is unbound
>
This one works:
class ['a] foo ch =
object
method myprintf (fmt : ('a, out_channel, unit) format) =
Printf.fprintf ch fmt
end
--
Nicolas Pouillard
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] class and printf
2006-10-30 12:42 class and printf Anastasia Gornostaeva
2006-10-30 12:47 ` [Caml-list] " Nicolas Pouillard
@ 2006-10-30 12:49 ` Jon Harrop
2006-10-30 13:21 ` Jacques Garrigue
2 siblings, 0 replies; 5+ messages in thread
From: Jon Harrop @ 2006-10-30 12:49 UTC (permalink / raw)
To: caml-list
On Monday 30 October 2006 12:42, Anastasia Gornostaeva wrote:
> What I need to drink to compile it?
>
> class foo ch =
> object
> method myprintf = Printf.fprintf ch
> end
>
> The compiler says
> The method myprintf has type ('a, out_channel, unit) format -> 'a where 'a
> is unbound
Bind 'a:
# class ['a] foo ch = object
method f : ('a, out_channel, unit) format -> 'a = Printf.fprintf ch
end;;
class ['a] foo :
out_channel -> object method f : ('a, out_channel, unit) format -> 'a end
--
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
Objective CAML for Scientists
http://www.ffconsultancy.com/products/ocaml_for_scientists
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] class and printf
2006-10-30 12:42 class and printf Anastasia Gornostaeva
2006-10-30 12:47 ` [Caml-list] " Nicolas Pouillard
2006-10-30 12:49 ` Jon Harrop
@ 2006-10-30 13:21 ` Jacques Garrigue
2 siblings, 0 replies; 5+ messages in thread
From: Jacques Garrigue @ 2006-10-30 13:21 UTC (permalink / raw)
To: ermine; +Cc: caml-list
From: Anastasia Gornostaeva <ermine@ermine.pp.ru>
> What I need to drink to compile it?
>
> class foo ch =
> object
> method myprintf = Printf.fprintf ch
> end
>
> The compiler says
> The method myprintf has type ('a, out_channel, unit) format -> 'a where 'a
> is unbound
Since I assume that you want to be able to use differently typed
formats with the same object,
class foo ch = object
method myprintf : 'a. ('a, out_channel, unit) format -> 'a =
Printf.fprintf ch
end
Jacques Garrigue
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] class and printf
2006-10-30 12:47 ` [Caml-list] " Nicolas Pouillard
@ 2006-10-30 13:28 ` Virgile Prevosto
0 siblings, 0 replies; 5+ messages in thread
From: Virgile Prevosto @ 2006-10-30 13:28 UTC (permalink / raw)
To: caml-list
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
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2006-10-30 13:28 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-10-30 12:42 class and printf Anastasia Gornostaeva
2006-10-30 12:47 ` [Caml-list] " Nicolas Pouillard
2006-10-30 13:28 ` Virgile Prevosto
2006-10-30 12:49 ` Jon Harrop
2006-10-30 13:21 ` Jacques Garrigue
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox