caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Olivier Andrieu <andrieu@ijm.jussieu.fr>
To: martin_jambon@emailuser.net
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Coercion of arrays of objects (and some other containers)
Date: Wed, 13 Apr 2005 02:49:24 +0200 (CEST)	[thread overview]
Message-ID: <20050413.024924.103869922.oandrieu@nerim.net> (raw)
In-Reply-To: <Pine.LNX.4.44.0504121702240.5206-100000@localhost>

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 > = <obj>
 > 
 > (* That is nice: *)
 > # ([ obj ] :> < a : unit > list);;
 > - : < a : unit > list = [<obj>]
 > 
 > (* 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


  reply	other threads:[~2005-04-13  0:49 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-04-13  0:14 Martin Jambon
2005-04-13  0:49 ` Olivier Andrieu [this message]
2005-04-13  0:59 ` [Caml-list] " Jacques Garrigue
2005-04-13  6:37   ` Matthieu Dubuget
2005-04-13 14:11     ` Olivier Andrieu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20050413.024924.103869922.oandrieu@nerim.net \
    --to=andrieu@ijm.jussieu.fr \
    --cc=caml-list@inria.fr \
    --cc=martin_jambon@emailuser.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).