Version française
Home     About     Download     Resources     Contact us    
Browse thread
Optional arguments
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jacques Garrigue <garrigue@k...>
Subject: Re: Optional arguments
From: Serguei Ouklonski <Sergeiu@optrak.co.uk>

> Example1:
> 
> let fn1 a:string (?b = "") = (String.length a) + (String.length b)
> 
> let x = fn1 "test"
> 
>   result is function
> 
> Example2:
> 
> let fn1 (?a = "") b:string = (String.length a) + (String.length b)
> 
> let x = fn1 "test"
> 
>   result is 4 (number)
> 
> It seems that in both cases result should be 4.
> 
> Have I missed something from OCaml docs?

Optional arguments are discarded when you apply a functions to a
non-labelled argument _appearing after them_ in the function's type.
So in your example, since a is taken before b, b will not be
discarded.
Since there is no way to discard b in this, the compiler warns you:
      # let fn1 a ?(b = "") = String.length a + String.length b;;
      Warning: This optional argument cannot be erased
      val fn1 : string -> ?b:string -> int = <fun>

Regards,

Jacques Garrigue
---------------------------------------------------------------------------
Jacques Garrigue      Kyoto University     garrigue at kurims.kyoto-u.ac.jp
		<A HREF=http://wwwfun.kurims.kyoto-u.ac.jp/~garrigue/>JG</A>