caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Optional arguments
@ 2008-11-09 19:17 malc
  2008-11-09 19:51 ` [Caml-list] " David Teller
  0 siblings, 1 reply; 5+ 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] 5+ messages in thread

* Re: [Caml-list] Optional arguments
  2008-11-09 19:17 Optional arguments malc
@ 2008-11-09 19:51 ` David Teller
  2008-11-10 20:52   ` malc
  0 siblings, 1 reply; 5+ messages in thread
From: David Teller @ 2008-11-09 19:51 UTC (permalink / raw)
  To: malc; +Cc: caml-list

What were you expecting?

Your definition of [a] always passes argument [i] to [b], so the default
value is never used.

Cheers,
 David

On Sun, 2008-11-09 at 22:17 +0300, malc wrote:
> 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?
> 
-- 
David Teller-Rajchenbach
 Security of Distributed Systems
  http://www.univ-orleans.fr/lifo/Members/David.Teller
 Angry researcher: French Universities need reforms, but the LRU act brings liquidations. 


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

* Re: [Caml-list] Optional arguments
  2008-11-09 19:51 ` [Caml-list] " David Teller
@ 2008-11-10 20:52   ` malc
  0 siblings, 0 replies; 5+ messages in thread
From: malc @ 2008-11-10 20:52 UTC (permalink / raw)
  To: David Teller; +Cc: caml-list

On Sun, 9 Nov 2008, David Teller wrote:

> What were you expecting?
>
> Your definition of [a] always passes argument [i] to [b], so the default
> value is never used.

Yes indeed, i probably should quit writing code this late, sorry for
the noise.

-- 
mailto:av1474@comtv.ru


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

* Re: Optional arguments
  2001-02-08 12:50 Serguei Ouklonski
@ 2001-02-09  1:19 ` Jacques Garrigue
  0 siblings, 0 replies; 5+ 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] 5+ messages in thread

* Optional arguments
@ 2001-02-08 12:50 Serguei Ouklonski
  2001-02-09  1:19 ` Jacques Garrigue
  0 siblings, 1 reply; 5+ 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] 5+ messages in thread

end of thread, other threads:[~2008-11-10 20:52 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-11-09 19:17 Optional arguments malc
2008-11-09 19:51 ` [Caml-list] " David Teller
2008-11-10 20:52   ` malc
  -- strict thread matches above, loose matches on Subject: below --
2001-02-08 12:50 Serguei Ouklonski
2001-02-09  1:19 ` 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).