caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Open class types
@ 2008-07-01 13:53 SerP
  2008-07-01 15:02 ` [Caml-list] " Tiphaine Turpin
  2008-07-01 15:14 ` Virgile Prevosto
  0 siblings, 2 replies; 3+ messages in thread
From: SerP @ 2008-07-01 13:53 UTC (permalink / raw)
  To: caml-list



class type location_t =
  object
    method a: int;
    method b: string;
  end;

class type session_t =
  object
    method id:int;
    method location: #location_t;
  end;

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

class location =
  object
    method a = 1;
    method b = "b";
    method c = ["a";"b"];
  end;

class session =
  object  
    value location = new location;
    method id = 1;
    method location = location;
  end;

value _ =
  let s = new session in
  check_session s;
====================================
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; .. >
       Type location = < a : int; b : string; c : string list >
       is not compatible with type 'b
       The second object type has no method c
--------------------------------------------
But,
    value check_session (session:< id : int; location : < a : int; b : 
string; .. >; .. >) = begin .....

Compiled properly.

Please help. Is exists any way to define open class type like inline 
type definition.
Write every function with inline type definition in mli files is terrible.



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

* Re: [Caml-list] Open class types
  2008-07-01 13:53 Open class types SerP
@ 2008-07-01 15:02 ` Tiphaine Turpin
  2008-07-01 15:14 ` Virgile Prevosto
  1 sibling, 0 replies; 3+ messages in thread
From: Tiphaine Turpin @ 2008-07-01 15:02 UTC (permalink / raw)
  To: caml-list

The behaviour of the compiler for your code is strange: I don't  see why 
the message error say the following:
    #session_t as 'a = < id : int; location : 'b. #location_t; .. >
where location is required to be a #location_t for all 'b (which I 
assume is the implicit self type variable of the open_type #location_t).
This seems to restrictive. Maybe a bug ?

Anymay, putting an explicit parameter in the session_t type solves the 
issue:

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

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

Tiphaine Turpin




SerP a écrit :
>
>
> class type location_t =
>  object
>    method a: int;
>    method b: string;
>  end;
>
> class type session_t =
>  object
>    method id:int;
>    method location: #location_t;
>  end;
>
> value check_session (session:#session_t) = begin
>  print_int session#id;
>  print_int session#location#a;
>  print_string session#location#b;
> end;
>
> class location =
>  object
>    method a = 1;
>    method b = "b";
>    method c = ["a";"b"];
>  end;
>
> class session =
>  object     value location = new location;
>    method id = 1;
>    method location = location;
>  end;
>
> value _ =
>  let s = new session in
>  check_session s;
> ====================================
> 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; .. >
>       Type location = < a : int; b : string; c : string list >
>       is not compatible with type 'b
>       The second object type has no method c
> --------------------------------------------
> But,
>    value check_session (session:< id : int; location : < a : int; b : 
> string; .. >; .. >) = begin .....
>
> Compiled properly.
>
> Please help. Is exists any way to define open class type like inline 
> type definition.
> Write every function with inline type definition in mli files is 
> terrible.
>
>
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>


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

* Re: [Caml-list] Open class types
  2008-07-01 13:53 Open class types SerP
  2008-07-01 15:02 ` [Caml-list] " Tiphaine Turpin
@ 2008-07-01 15:14 ` Virgile Prevosto
  1 sibling, 0 replies; 3+ messages in thread
From: Virgile Prevosto @ 2008-07-01 15:14 UTC (permalink / raw)
  To: caml-list

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


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

end of thread, other threads:[~2008-07-01 15:15 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-07-01 13:53 Open class types SerP
2008-07-01 15:02 ` [Caml-list] " Tiphaine Turpin
2008-07-01 15:14 ` Virgile Prevosto

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).