caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Optional arguments
@ 2001-02-08 12:50 Serguei Ouklonski
  2001-02-09  1:19 ` Jacques Garrigue
  0 siblings, 1 reply; 3+ messages in thread
From: Serguei Ouklonski @ 2001-02-08 12:50 UTC (permalink / raw)
  To: caml-list

Hi,

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?


Regards

Serguei Ouklonski



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

* Re: Optional arguments
  2001-02-08 12:50 Optional arguments Serguei Ouklonski
@ 2001-02-09  1:19 ` Jacques Garrigue
  0 siblings, 0 replies; 3+ messages in thread
From: Jacques Garrigue @ 2001-02-09  1:19 UTC (permalink / raw)
  To: Sergeiu; +Cc: caml-list

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>



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

* Optional arguments
@ 2008-11-09 19:17 malc
  0 siblings, 0 replies; 3+ messages in thread
From: malc @ 2008-11-09 19:17 UTC (permalink / raw)
  To: caml-list

         Objective Caml version 3.10.0

# let a i = let b ?(i=i mod 3) () = i in b ~i ();;
val a : int -> int = <fun>
# for i = 0 to 5 do print_int (a i); done;;
012345- : unit = ()

Is this something to be expected? Or perhaps something which calls
for an upgrade?

-- 
mailto:av1474@comtv.ru


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

end of thread, other threads:[~2008-11-09 19:17 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-02-08 12:50 Optional arguments Serguei Ouklonski
2001-02-09  1:19 ` Jacques Garrigue
2008-11-09 19:17 malc

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