caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Polymorphic method and polymorphic type
@ 2008-03-17 10:21 SerP
  2008-03-17 12:26 ` [Caml-list] " SerP
  2008-03-17 12:37 ` Jacques Garrigue
  0 siblings, 2 replies; 4+ messages in thread
From: SerP @ 2008-03-17 10:21 UTC (permalink / raw)
  To: Caml-list, caml-list-bounces

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?


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

* Re: [Caml-list] Polymorphic method and polymorphic type
  2008-03-17 10:21 Polymorphic method and polymorphic type SerP
@ 2008-03-17 12:26 ` SerP
  2008-03-17 12:51   ` Jacques Garrigue
  2008-03-17 12:37 ` Jacques Garrigue
  1 sibling, 1 reply; 4+ messages in thread
From: SerP @ 2008-03-17 12:26 UTC (permalink / raw)
  To: Caml-list, caml-list-bounces

but if we use simple variant type instead of polymorphic variants it 
works perfect


type 'a get_mode = [ Read | Watch of (#getter as 'a)];;


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

* Re: [Caml-list] Polymorphic method and polymorphic type
  2008-03-17 10:21 Polymorphic method and polymorphic type SerP
  2008-03-17 12:26 ` [Caml-list] " SerP
@ 2008-03-17 12:37 ` Jacques Garrigue
  1 sibling, 0 replies; 4+ messages in thread
From: Jacques Garrigue @ 2008-03-17 12:37 UTC (permalink / raw)
  To: serp; +Cc: Caml-list

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


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

* Re: [Caml-list] Polymorphic method and polymorphic type
  2008-03-17 12:26 ` [Caml-list] " SerP
@ 2008-03-17 12:51   ` Jacques Garrigue
  0 siblings, 0 replies; 4+ messages in thread
From: Jacques Garrigue @ 2008-03-17 12:51 UTC (permalink / raw)
  To: serp; +Cc: Caml-list

From: SerP <serp@stork.ru>

> but if we use simple variant type instead of polymorphic variants it 
> works perfect
> 
> 
> type 'a get_mode = [ Read | Watch of (#getter as 'a)];;

(Better to avoid revised syntax in such cases, it is confusing here)

I was not aware of that, but this is logical since the problem comes
from expansion, and simple variants do not need to be expanded.
So I should correct my phrasing: explicit polymorphic type variables
inside constrained type parameters of type abbreviations are not
supported.

Jacques Garrigue


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

end of thread, other threads:[~2008-03-17 12:52 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-03-17 10:21 Polymorphic method and polymorphic type SerP
2008-03-17 12:26 ` [Caml-list] " SerP
2008-03-17 12:51   ` Jacques Garrigue
2008-03-17 12:37 ` 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).