From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Delivered-To: caml-list@yquem.inria.fr Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by yquem.inria.fr (Postfix) with ESMTP id 1F48FBC84 for ; Wed, 13 Apr 2005 02:58:50 +0200 (CEST) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id j3D0wnD4018467 for ; Wed, 13 Apr 2005 02:58:49 +0200 Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id CAA27085 for ; Wed, 13 Apr 2005 02:58:48 +0200 (MET DST) Received: from kurims.kurims.kyoto-u.ac.jp (kurims.kurims.kyoto-u.ac.jp [130.54.16.1]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id j3D0wk6Y012095 for ; Wed, 13 Apr 2005 02:58:48 +0200 Received: from localhost (suiren [130.54.16.25]) by kurims.kurims.kyoto-u.ac.jp (8.13.1/8.13.1) with ESMTP id j3D0wgM9010469; Wed, 13 Apr 2005 09:58:42 +0900 (JST) Date: Wed, 13 Apr 2005 09:59:00 +0900 (JST) Message-Id: <20050413.095900.07644780.garrigue@math.nagoya-u.ac.jp> To: martin_jambon@emailuser.net Cc: caml-list@inria.fr Subject: Re: [Caml-list] Coercion of arrays of objects (and some other containers) From: Jacques Garrigue In-Reply-To: References: X-Mailer: Mew version 4.2 on Emacs 21.3 / Mule 5.0 (SAKAKI) Mime-Version: 1.0 Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Miltered: at concorde with ID 425C6EC9.001 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Miltered: at nez-perce with ID 425C6EC6.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; caml-list:01 arrays:01 val:01 arrays:01 coerced:01 subtyping:01 runtime:01 hash:01 hash:01 val:01 hashtbl:01 hashtbl:01 bool:01 subtyping:01 ...:98 X-Spam-Checker-Version: SpamAssassin 3.0.2 (2004-11-16) on yquem.inria.fr X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.0.2 X-Spam-Level: From: Martin Jambon > Here is my problem: > > # let obj = > object > method a = () > method b = () > end;; > val obj : < a : unit; b : unit > = > > (* That is nice: *) > # ([ obj ] :> < a : unit > list);; > - : < a : unit > list = [] > > (* But why doesn't it work with arrays? *) > # ([| obj |] :> < a : unit > array);; > Characters 1-10: > ([| obj |] :> < a : unit > array);; > ^^^^^^^^^ > This expression cannot be coerced to type < a : unit > array; it has type > < a : unit; b : unit > array > but is here used with type < a : unit > array > Only the first object type has a method b Suppose that it worked. Then you could have this scenario. let arr = [|obj|];; let arr' = (arr :> array);; arr'.(0) <- object method a = () end;; arr.(0)#b;; Segmentation fault. Such subtyping is allowed in Java, but this is an unsafe part of the type system, which requires time-consuming runtime checks. > In practice I have this problem with a hash table of objects, and I > expected it to work since it works fine with lists of the same > type of objects... > Is there any workaround? If you don't need to add objects to this hash table, this is possible with the following approach: class ['a,+'b] table tbl = object val tbl : ('a,'b) Hashtbl.t = tbl method find = Hashtbl.find tbl method find_all = Hashtbl.find_all tbl method mem = Hashtbl.mem tbl end (* class ['a, 'b] table : ('a, 'b) Hashtbl.t -> object val tbl : ('a, 'b) Hashtbl.t method find : 'a -> 'b method find_all : 'a -> 'b list method mem : 'a -> bool end *) let coerce tbl = (tbl : ('a,) table :> ('a,) table) See that 'b appears only in covariant positions, allowing its subtyping. Jacques Garrigue