caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Guillaume Yziquel <guillaume.yziquel@citycable.ch>
To: Jacques Garrigue <garrigue@math.nagoya-u.ac.jp>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] polymorphic method.
Date: Thu, 10 Sep 2009 15:05:10 +0200	[thread overview]
Message-ID: <4AA8F986.1060206@citycable.ch> (raw)
In-Reply-To: <20090910.214827.104047449.garrigue@math.nagoya-u.ac.jp>

Jacques Garrigue a écrit :
> There are already polymorphic methods in ocaml.
> The syntax for your example would be:
> 
> class myobject = object
>   method id : 'a. 'a -> 'a = fun x -> x
> end

Yes, I know that there are already polymorphic methods in ocaml.

> A "polymorphic" keyword might seem simpler, but it would be complex to
> handle the case where a polymorphic method type contains also class
> parameters:
> 
> class ['a] cell (x : 'a) = object
>   method pair : 'b. 'b -> 'a * 'b = fun y -> (x,y)
> end

Indeed. But you could also write:

class ['a] cell (x: 'a) = object
   polymorphic method pair y = (x, y)
end

The polymorphic keyword would only bind what can be bind. Since x is 
already of type 'a, it would escape the scope of the 'polymorphic' 
keyword. But y would not escape the scope of the polymorphic keyword.

> More generally, you might end up with types more polymorphic than you
> expected, and since differently instantiated polymorphic method types
> are incompatible, this would be a problem.

Well, for now, when I write methods, my methods tend to be not 
polymorphic enough. One could keep the default behaviour without the 
keyword, and also use the keyword polymorphic to bind only what can be 
bound (i.e. not 'x' in your example).

The problem I am facing now is cumbersome: writing types for methods 
arguments everywhere... That's not what people would expect for type 
inference, unfortunately. The 'polymorphic' keyword would only be a hint 
as to how type inference would be done. This way I wouldn't have to keep 
the typing of the method arguments in sync with the code of the method 
(or at least, much less).

By the way, I do not exactly understand the "you might end up with types 
more polymorphic than you expected" part.

> Jacques Garrigue

-- 
      Guillaume Yziquel
http://yziquel.homelinux.org/


  reply	other threads:[~2009-09-10 13:09 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <4AA191F3.5000004@yziquel.homelinux.org>
2009-09-10 12:30 ` Guillaume Yziquel
2009-09-10 12:30 ` Guillaume Yziquel
2009-09-10 12:48   ` [Caml-list] " Jacques Garrigue
2009-09-10 13:05     ` Guillaume Yziquel [this message]
2009-09-10 14:21       ` Jacques Garrigue
2009-09-10 19:14         ` Guillaume Yziquel
2009-09-11  7:14           ` blue storm
2009-09-11  8:20             ` Guillaume Yziquel
2009-09-11  8:18           ` Jacques Garrigue
2009-09-11  9:40             ` Guillaume Yziquel

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=4AA8F986.1060206@citycable.ch \
    --to=guillaume.yziquel@citycable.ch \
    --cc=caml-list@inria.fr \
    --cc=garrigue@math.nagoya-u.ac.jp \
    /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).