From: Jacques Garrigue <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)
Date: Wed, 13 Apr 2005 09:59:00 +0900 (JST) [thread overview]
Message-ID: <20050413.095900.07644780.garrigue@math.nagoya-u.ac.jp> (raw)
In-Reply-To: <Pine.LNX.4.44.0504121702240.5206-100000@localhost>
From: Martin Jambon <martin_jambon@emailuser.net>
> 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
Suppose that it worked.
Then you could have this scenario.
let arr = [|obj|];;
let arr' = (arr :> <a:unit> 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,<a:int;b:int>) table :> ('a,<a:int>) table)
See that 'b appears only in covariant positions, allowing its
subtyping.
Jacques Garrigue
next prev parent reply other threads:[~2005-04-13 0:58 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 ` [Caml-list] " Olivier Andrieu
2005-04-13 0:59 ` Jacques Garrigue [this message]
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.095900.07644780.garrigue@math.nagoya-u.ac.jp \
--to=garrigue@math.nagoya-u.ac.jp \
--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).