caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Typing of default arguments
@ 2001-02-16  1:54 Patrick M Doane
  2001-02-17  7:15 ` Pierre Weis
  0 siblings, 1 reply; 4+ messages in thread
From: Patrick M Doane @ 2001-02-16  1:54 UTC (permalink / raw)
  To: caml-list

I am having difficulty understanding the typing rules for default
arguments. Here are some test cases:

  (* No default arguments: all of these work *)
  let f ~g x = g x

  let a = f (fun x -> x) ()
  let b = f (fun x -> (x,x)) ()

  let c = f ~g:(fun x -> x) ()
  let d = f ~g:(fun x -> (x,x)) ()

  (* Default argument: last one fails to type-check *)
  let f' ?(g = (fun x -> x)) x = g x

  let a' = f' (fun x -> x) ()
  let b' = f' (fun x -> (x,x)) ()

  let c' = f' ~g:(fun x -> x) ()
  let d' = f' ~g:(fun x -> (x,x)) ()

If I remove the offending case at the end, I notice that the difference
between f and f' is:

  val f : g:('a -> 'b) -> 'a -> 'b
  val f' : ?g:('a -> 'a) -> 'a -> 'a

So why does the definition for b' typecheck properly? The type of the
first argument is not 'a -> 'a but rather 'a -> 'a * 'a. 

It's unfortunate that supplying a default argument restricts the
polymorphism and reuse of the function. I would like someway to provide
the default without losing that polymorphic capability. I think this
example shows that is should at least be legal to do (from the declaration
of b').

Any ideas?

Thanks,
Patrick Doane

-------------------
To unsubscribe, mail caml-list-request@inria.fr.  Archives: http://caml.inria.fr


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2001-02-19  0:02 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-02-16  1:54 [Caml-list] Typing of default arguments Patrick M Doane
2001-02-17  7:15 ` Pierre Weis
2001-02-17 20:07   ` Patrick M Doane
2001-02-19  0:01     ` Jacques Garrigue

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).