Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[Caml-list] Typing of default arguments
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Patrick M Doane <patrick@w...>
Subject: [Caml-list] Typing of default arguments
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?

Patrick Doane

To unsubscribe, mail  Archives: