Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Fwd: Polymorphic optional label argument, with default
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Richard Jones <rich@a...>
Subject: Re: [Caml-list] Fwd: Polymorphic optional label argument, with default
On Sun, Apr 11, 2004 at 01:26:24AM -0500, Brian Hurt wrote:
> >   plot : ?labels : ('a -> string) -> (* ... 'a ... *) -> unit
> > 
> > [The 'a types are the same type.  If I leave out the initializer, then
> > it works.]
> > 
> > How to?
> > 
> I don't think it's possible.  Consider the following situation: I pass in 
> a graph of floats for example, and then forget to specify a labels 
> argument.  Now you're passing a float to string_of_int.

I'd want this to generate a compile-time error, because the 'a 's
aren't the same.

It's perfectly possible to define this function if one leaves out the
default argument, or sets the default argument to a function typed as
'a -> string.

# let plot ~labels graph = print_endline (labels graph);;
val plot : labels:('a -> string) -> 'a -> unit = <fun>


# let string_of_any x = "foo";;
val string_of_any : 'a -> string = <fun>
# let plot ?(labels = string_of_any) graph = print_endline (labels graph);;
val plot : ?labels:('a -> string) -> 'a -> unit = <fun>

But not if I want the default to be the most common case (which is
that my graph will be a graph of ints).  I want the common case in
there so that most of the time end users of the API won't need to
worry about ~labels and optional arguments.


Richard Jones.
Merjis Ltd. - improving website return on investment
'There is a joke about American engineers and French engineers. The
American team brings a prototype to the French team. The French team's
response is: "Well, it works fine in practice; but how will it hold up
in theory?"'

To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: