caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jacques Garrigue <garrigue@math.nagoya-u.ac.jp>
To: serp@stork.ru
Cc: Caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Polymorphic method and polymorphic type
Date: Mon, 17 Mar 2008 21:37:28 +0900 (JST)	[thread overview]
Message-ID: <20080317.213728.191427528.garrigue@math.nagoya-u.ac.jp> (raw)
In-Reply-To: <47DE4635.7050101@stork.ru>

From: SerP <serp@stork.ru>
> Simple example. All functions are fake.
> 
> class getter = object method add_some p = p+1 end;;
> type 'a get_mode = [ `Read | `Watch of (#getter as 'a)];;
> let string_of_get_mode (gm: 'a get_mode) = match gm with `Read -> "Read" 
> | `Watch g -> let i = g#add_some 1 in ("Watch "^ (string_of_int i));;
> class c = object method pgm : 'a. ('a get_mode) -> string = 
> string_of_get_mode end;;
> --------------
> This expression has type 'a. (#getter as 'a) get_mode -> string
> but is here used with type 'b. (#getter as 'b) get_mode -> string
> Type #getter as 'c = < add_some : int -> int; .. >
> is not compatible with type 'c
> 
> ====================
> Please help. Is it my error? how should we do that the right way?

Unfortunately, explicitly polymorphic type variables inside
constrained type parameters are not allowed. This is due to the way
type expansion is done inside the type checker...
It should be possible to solve it, but I'm afraid lots of places would
have to be modified to provide comprehensive support.

Currently, the way to do it is to avoid constrained type parameters.

type 'a get_mode = [ `Read | `Watch of 'a];;
let string_of_get_mode (gm: #getter get_mode) =
   match gm with `Read -> "Read" 
  | `Watch g -> let i = g#add_some 1 in ("Watch "^ (string_of_int i));;
class c = object
 method pgm : 'a. (#getter as 'a) get_mode -> string = string_of_get_mode
end;;

This should not be too difficult usually. However, there is no
workaround if the type with the constraint is abstract.

Jacques Garrigue


      parent reply	other threads:[~2008-03-17 12:37 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-03-17 10:21 SerP
2008-03-17 12:26 ` [Caml-list] " SerP
2008-03-17 12:51   ` Jacques Garrigue
2008-03-17 12:37 ` Jacques Garrigue [this message]

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=20080317.213728.191427528.garrigue@math.nagoya-u.ac.jp \
    --to=garrigue@math.nagoya-u.ac.jp \
    --cc=Caml-list@yquem.inria.fr \
    --cc=serp@stork.ru \
    /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).