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 BB7D2BC84 for ; Wed, 13 Apr 2005 02:49:26 +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 j3D0nQMq017397 for ; Wed, 13 Apr 2005 02:49:26 +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 CAA26955 for ; Wed, 13 Apr 2005 02:49:26 +0200 (MET DST) Received: from kraid.nerim.net (smtp-103-wednesday.nerim.net [62.4.16.103]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id j3D0nPeX011152 for ; Wed, 13 Apr 2005 02:49:25 +0200 Received: from localhost (karryall.dnsalias.org [62.4.18.180]) by kraid.nerim.net (Postfix) with ESMTP id 424E1418FB; Wed, 13 Apr 2005 02:49:23 +0200 (CEST) Date: Wed, 13 Apr 2005 02:49:24 +0200 (CEST) Message-Id: <20050413.024924.103869922.oandrieu@nerim.net> To: martin_jambon@emailuser.net Cc: caml-list@inria.fr Subject: Re: [Caml-list] Coercion of arrays of objects (and some other containers) From: Olivier Andrieu In-Reply-To: References: X-Mailer: Mew version 4.1 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 425C6C96.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Miltered: at nez-perce with ID 425C6C95.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; caml-list:01 arrays:01 andrieu:01 andrieu:01 ijm:01 val:01 arrays:01 coerced:01 variance:01 hash:01 ...:98 workaround:01 expression:01 jussieu:01 coercion:01 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: Salut Martin, > Martin Jambon [Tue, 12 Apr 2005]: > 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 It's a variance problem : a list is covariant so the coercion works. But an array is invariant (because of the Array.set operations). Otherwise after coercion you would be able to do store in the array an object without a `b' method, this object being also reachable through the original array. > 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? Maybe this : Array.map (fun o -> (o :> < a : unit >)) [| obj |] -- Olivier