caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Andreas Rossberg" <AndreasRossberg@web.de>
To: "O'Caml Mailing List" <caml-list@inria.fr>
Subject: Re: [Caml-list] Unexpected '_a problem
Date: Tue, 1 Aug 2006 23:20:00 +0200	[thread overview]
Message-ID: <004801c6b5b0$47811030$15b2a8c0@wiko> (raw)
In-Reply-To: <875c7e070608011347y62d2dd21lc55390cd4ffd00a1@mail.gmail.com>

"Chris King" <colanderman@gmail.com> wrote:
>
> # let create_foo () = object method foo (_: 'a) = () end;;
> val create_foo : unit -> < foo : 'a -> unit > = <fun>
> # let a = create_foo ();;
> val a : < bar : '_a -> unit > = <obj>
>
> the compiler determines a is monomorphic, since 'a is in a
> contravariant position.  But why, when 'a is placed in a covariant
> position:

This has nothing to do with contravariance, nor with subtyping or objects at
all. What you observe is the infamous "value restriction": roughly, a
definition can only be polymorphic when its right-hand side is syntactically
a value (i.e. a function, tuple, constant, etc or combination thereof). In
your case it's an application.

Also note that in the above code, it is the object that would be
polymorphic, not the method! If you want a polymorphic method, the proper
syntax is the following:

# let create_foo () = object method foo : 'a.'a -> unit = fun _ -> () end;;
val create_foo : unit -> < foo : 'a. 'a -> unit > = <fun>
# let a = create_foo ();;
val a : < foo : 'a. 'a -> unit > = <obj>
# a#foo 5; a#foo true;;
- : unit = ()

Here, `a' is a monomorphic object with a polymorphic method.

- Andreas


  reply	other threads:[~2006-08-01 21:15 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-08-01 20:47 Chris King
2006-08-01 21:20 ` Andreas Rossberg [this message]
2006-08-01 22:09   ` [Caml-list] " Remi Vanicat
2006-08-02  7:00     ` Alain Frisch
2006-08-02 17:57       ` Chris King
2006-08-04  2:42         ` Jacques Garrigue
2006-08-04 14:18           ` Chris King
2006-08-02  7:03     ` Christophe Dehlinger
2006-08-02  8:07       ` Andreas Rossberg

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='004801c6b5b0$47811030$15b2a8c0@wiko' \
    --to=andreasrossberg@web.de \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).