caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Labeled arguments and polymorphic return types
@ 2007-08-11 18:17 Andre Nathan
  2007-08-13  8:51 ` [Caml-list] " Jacques Garrigue
  0 siblings, 1 reply; 3+ messages in thread
From: Andre Nathan @ 2007-08-11 18:17 UTC (permalink / raw)
  To: caml-list

Hello

Consider the following definition:

# let f ~a = print_int a; raise Exit;;
val f : a:int -> 'a = <fun>

If I call f without using the argument label, I get the following
warning:

# f 1;;
Warning X: this argument will not be used by the function.
- : a:int -> 'a = <fun>

The call works if I use the label ("f ~a:1"), if I define f without a
label for its argument, or if I remove the "raise" statement (causing
the return value to be unit instead of 'a).

Why is this warning generated?

Thanks in advance,
Andre


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

* Re: [Caml-list] Labeled arguments and polymorphic return types
  2007-08-11 18:17 Labeled arguments and polymorphic return types Andre Nathan
@ 2007-08-13  8:51 ` Jacques Garrigue
  2007-08-13 15:53   ` Andre Nathan
  0 siblings, 1 reply; 3+ messages in thread
From: Jacques Garrigue @ 2007-08-13  8:51 UTC (permalink / raw)
  To: andre; +Cc: caml-list

From: Andre Nathan <andre@digirati.com.br>

> Consider the following definition:
> 
> # let f ~a = print_int a; raise Exit;;
> val f : a:int -> 'a = <fun>
> 
> If I call f without using the argument label, I get the following
> warning:
> 
> # f 1;;
> Warning X: this argument will not be used by the function.
> - : a:int -> 'a = <fun>
> 
> The call works if I use the label ("f ~a:1"), if I define f without a
> label for its argument, or if I remove the "raise" statement (causing
> the return value to be unit instead of 'a).
> 
> Why is this warning generated?

Reading your description, I think you are misunderstanding the
semantics of labels. Namely, you are only allowed to omit label names
if all the arguments are known to be passed, which in particular
excludes all functions whose return type is 'a. In that case, a
non-labelled argument is assumed to correspond to a (yet unknown)
non-labelled formal parameter. 

The above case is tricky, but here is a simple one:
# let f ~a x = a :: x;;
val f : a:'a -> 'a list -> 'a list = <fun>
# f 1 [];;
- : int list = [1]
# f [];; 
- : a:'a -> 'a list = <fun>
In the last line, [] does not replace ~a but x.

Now, this explains the above warning: since there is no unlabelled
parameter, but the return type is polymorphic, your unlabelled
argument will be simply discarded, which is probably not what you
wanted. The compiler is kind enough to inform you.

Jacques Garrigue


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

* Re: [Caml-list] Labeled arguments and polymorphic return types
  2007-08-13  8:51 ` [Caml-list] " Jacques Garrigue
@ 2007-08-13 15:53   ` Andre Nathan
  0 siblings, 0 replies; 3+ messages in thread
From: Andre Nathan @ 2007-08-13 15:53 UTC (permalink / raw)
  To: Jacques Garrigue; +Cc: caml-list

On Mon, 2007-08-13 at 17:51 +0900, Jacques Garrigue wrote:
> Now, this explains the above warning: since there is no unlabelled
> parameter, but the return type is polymorphic, your unlabelled
> argument will be simply discarded, which is probably not what you
> wanted. The compiler is kind enough to inform you.

Thanks! Your explanation makes it very clear.

Andre


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

end of thread, other threads:[~2007-08-13 15:54 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-08-11 18:17 Labeled arguments and polymorphic return types Andre Nathan
2007-08-13  8:51 ` [Caml-list] " Jacques Garrigue
2007-08-13 15:53   ` Andre Nathan

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