Version française
Home     About     Download     Resources     Contact us    
Browse thread
Open class types
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Virgile Prevosto <virgile.prevosto@m...>
Subject: Re: [Caml-list] Open class types
Hello,

Le mar 01 jui 2008 17:53:34 CEST,
SerP <serp@stork.ru> a écrit :

> class type session_t =
>   object
>     method id:int;
>     method location: #location_t;
>   end;
> 
...
> 
> Error: This expression has type session = < id : int; location :
> location > but is here used with type
>          #session_t as 'a = < id : int; location : 'b.
> #location_t; .. >

My guess is that session_t is a bit too polymorphic: location is
a polymorphic method (indicated by the 'b. in the error message). Thus,
a class implementing session_t should have a location method able to
return any subtype of location_t, which might be a bit complicated to
say the least. A more sensible class type would require that the
location method returns a subtype of location_t, which may differ from
one implementation to the other, but is fixed for a given
implementation. By the way, this matches more or less your second
solution.
At the class/class type level, this can be done by adding a type
parameter to the class type and using a constraint as shown below
(warning, this is the regular ocaml syntax, not the revised one):

class type ['a]session_t =
  object
    method id:int
    method location: 'a
    constraint 'a = #location_t
  end

let check_session (session:'a #session_t) = begin
  print_int session#id;
  print_int session#location#a;
  print_string session#location#b;
end

-- 
E tutto per oggi, a la prossima volta.
Virgile