From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@sympa.inria.fr Delivered-To: caml-list@sympa.inria.fr Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by sympa.inria.fr (Postfix) with ESMTPS id D0B0D7F616 for ; Wed, 22 Feb 2017 18:43:29 +0100 (CET) Authentication-Results: mail2-smtp-roc.national.inria.fr; spf=None smtp.pra=nicolas.ojeda.bar@lexifi.com; spf=None smtp.mailfrom=nicolas.ojeda.bar@lexifi.com; spf=None smtp.helo=postmaster@vrout10.yaziba.net Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of nicolas.ojeda.bar@lexifi.com) identity=pra; client-ip=185.56.204.32; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="nicolas.ojeda.bar@lexifi.com"; x-sender="nicolas.ojeda.bar@lexifi.com"; x-conformance=sidf_compatible Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of nicolas.ojeda.bar@lexifi.com) identity=mailfrom; client-ip=185.56.204.32; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="nicolas.ojeda.bar@lexifi.com"; x-sender="nicolas.ojeda.bar@lexifi.com"; x-conformance=sidf_compatible Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of postmaster@vrout10.yaziba.net) identity=helo; client-ip=185.56.204.32; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="nicolas.ojeda.bar@lexifi.com"; x-sender="postmaster@vrout10.yaziba.net"; x-conformance=sidf_compatible IronPort-PHdr: =?us-ascii?q?9a23=3AptbcBRETByyS63kAdAA1LJ1GYnF86YWxBRYc798d?= =?us-ascii?q?s5kLTJ75o8SwAkXT6L1XgUPTWs2DsrQf2reQ6v6rADRZqdbZ6TZZL8wKD0dEwe?= =?us-ascii?q?wt3CUeQ+e9QXXhK/DrayFoVO9jb3RCu0+BDE5OBczlbEfTqHDhpRQbGxH4KBYn?= =?us-ascii?q?br+tQt2ap42N2uuz45zeZRlTzHr4OOsqbUaAlhjKrsQdnadlL68wzFOJ/ioJKK?= =?us-ascii?q?xqwjYiH1OYnx/m6o+PursluwZXvf86vYYUS6Txf4wxS7pVHDUvdWcv65u4mwPE?= =?us-ascii?q?SF6p53AbVuwNpT1KDgTM2zt2RN+ltCrxse50w2+dP8D6RPY+UC6K66p7SRHpzi?= =?us-ascii?q?wAMmhqoynslsVsgfcD81qarBtlztuRPdiY?= X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: =?us-ascii?q?A0CkAQDGzK1YhyDMOLleGgEBAQECAQEBA?= =?us-ascii?q?QgBAQEBFAEBAQEBAQEBAQEBBwEBAQEBhAeBCQeDVJtiEIIqikOKRCqFeAKCfgd?= =?us-ascii?q?DFAEBAQEBAQEBAQEBEgEBAQgNCQodL4IzBAEdBYIWAQEBAwEjBEcLBQsLCw0NH?= =?us-ascii?q?QICIhIBBQEKEgYTEolLAw0IBAEJok0/jAOBbDqHQAODdAEBAQEBBQEBAQEBASK?= =?us-ascii?q?LO4dagl8FhzgMgU6GOoYahiqGdIsvgk6OQpFeFB6BEwIPJ4EhVFMXBYN2SoFud?= =?us-ascii?q?IhFgU8BAQE?= X-IPAS-Result: =?us-ascii?q?A0CkAQDGzK1YhyDMOLleGgEBAQECAQEBAQgBAQEBFAEBAQE?= =?us-ascii?q?BAQEBAQEBBwEBAQEBhAeBCQeDVJtiEIIqikOKRCqFeAKCfgdDFAEBAQEBAQEBA?= =?us-ascii?q?QEBEgEBAQgNCQodL4IzBAEdBYIWAQEBAwEjBEcLBQsLCw0NHQICIhIBBQEKEgY?= =?us-ascii?q?TEolLAw0IBAEJok0/jAOBbDqHQAODdAEBAQEBBQEBAQEBASKLO4dagl8FhzgMg?= =?us-ascii?q?U6GOoYahiqGdIsvgk6OQpFeFB6BEwIPJ4EhVFMXBYN2SoFudIhFgU8BAQE?= X-IronPort-AV: E=Sophos;i="5.35,195,1484002800"; d="scan'208,217";a="261685934" Received: from vrout10.yaziba.net ([185.56.204.32]) by mail2-smtp-roc.national.inria.fr with ESMTP/TLS/DHE-RSA-AES256-SHA; 22 Feb 2017 18:43:28 +0100 Received: from mtaout10.int.yaziba.net (mtaout10.int.yaziba.net [10.4.20.36]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by vrout10.yaziba.net (mx10.yaziba.net) with ESMTPS id 2F23352130 for ; Wed, 22 Feb 2017 18:43:28 +0100 (CET) Received: from localhost (localhost [127.0.0.1]) by mtaout10.int.yaziba.net (Postfix) with ESMTP id 42773160927 for ; Wed, 22 Feb 2017 18:43:28 +0100 (CET) X-Virus-Scanned: amavisd-new at Received: from mtaout10.int.yaziba.net ([127.0.0.1]) by localhost (mtaout10.int.yaziba.net [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id bLVueTehoGOi for ; Wed, 22 Feb 2017 18:43:28 +0100 (CET) Received: from mail-qt0-f173.google.com (mail-qt0-f173.google.com [209.85.216.173]) by mtaout10.int.yaziba.net (Postfix) with ESMTPSA id E4B5B160895 for ; Wed, 22 Feb 2017 18:43:27 +0100 (CET) Received: by mail-qt0-f173.google.com with SMTP id x35so8776768qtc.2 for ; Wed, 22 Feb 2017 09:43:27 -0800 (PST) X-Gm-Message-State: AMke39mlNX/+sUlYft7BlgMVb9Q9dfWgxBzzVFAbpUETKUAE0RfPTwDmsGP/wt7vBXqztPtzKlNV8NwpLZbNMg== X-Received: by 10.200.49.230 with SMTP id i35mr10451122qte.254.1487785406877; Wed, 22 Feb 2017 09:43:26 -0800 (PST) MIME-Version: 1.0 Received: by 10.237.56.225 with HTTP; Wed, 22 Feb 2017 09:43:06 -0800 (PST) In-Reply-To: <20170222173645.GF28111@annexia.org> References: <20170222170101.GE28111@annexia.org> <20170222173645.GF28111@annexia.org> From: =?UTF-8?Q?Nicol=C3=A1s_Ojeda_B=C3=A4r?= Date: Wed, 22 Feb 2017 18:43:06 +0100 X-Gmail-Original-Message-ID: Message-ID: To: "Richard W.M. Jones" Cc: Gabriel Scherer , caml users Content-Type: multipart/alternative; boundary=001a11c17f62ade479054922081d X-DRWEB-SCAN: ok X-VRSPAM-SCORE: -51 X-VRSPAM-STATE: legit X-VRSPAM-CAUSE: gggruggvucftvghtrhhoucdtuddrfeelhedrudeigddutdejucetufdoteggodetrfcurfhrohhfihhlvgemucggtfgfnhhsuhgsshgtrhhisggvnecuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenogfuuhhsphgvtghtffhomhgrihhnucdlgeelmdenucfjughrpegjfhfhfffkuffvtgesrgdtreertddtjeenucfhrhhomheppfhitgholhojshgpqfhjvggurggpueomrhcuoehnihgtohhlrghsrdhojhgvuggrrdgsrghrsehlvgigihhfihdrtghomheqnecuffhomhgrihhnpehtvghsthdrmhhlpdhlihhsthgpfhhnshdrmhhlpdhinhhrihgrrdhfrhdphigrhhhoohdrtghomhenucfkphepvddtledrkeehrddvudeirddujeefnecurfgrrhgrmhepmhhouggvpehsmhhtphhouhht X-VRSPAM-EXTCAUSE: mhhouggvpehsmhhtphhouhht Subject: Re: [Caml-list] List of structurally typed objects --001a11c17f62ade479054922081d Content-Type: text/plain; charset=UTF-8 Hi Richard, Note that you can still write < hello: string; .. > in the signature of register_obj and do the cast before adding your object to the list: let register_obj name obj = objs := (name, (obj :> < hello: string >)) :: !objs This way you do not need to downcast at each call-site of register_obj. Cheers, Nicolas On Wed, Feb 22, 2017 at 6:36 PM, Richard W.M. Jones wrote: > On Wed, Feb 22, 2017 at 12:09:37PM -0500, Gabriel Scherer wrote: > > The signature you demand is actually incorrect, because the return > > type of get_obj is to permissive: it claims that it can return *any* > > ('a obj), so you could register an object with just the hello method > > and get back an object with more methods. > > > > On the other hand, if you use the monomorphic < hello : string > type > > in the interface, then it is type-correct and the implementation you > > provide is accepted. It is explicit in the fact that you can't recover > > the extra methods of the object once they have been stored in this > > lowest-common-denominator container. > > Ah, I see. > > Modifying the code to use the monomorphic type works, but I have to > downcast the objects to the right type. For reference, the working > code is below. > > Thanks, > > Rich. > > list_fns.mli -------------------------------------------------------- > type obj = < hello : string > > val register_obj : string -> obj -> unit > val get_obj : string -> obj > > list_fns.ml --------------------------------------------------------- > type obj = < hello : string > > let objs = ref [] > let register_obj name obj = objs := (name, obj) :: !objs > let get_obj name = List.assoc name !objs > > test.ml ------------------------------------------------------------- > class foo = object > method hello = "hello from foo" > method goodbye () = print_endline "goodbye" > end > > class bar = object > method hello = "hello from bar" > end > > let () = > let o1 = new foo in > let o2 = new bar in > List_fns.register_obj "o1" (o1 :> List_fns.obj); > List_fns.register_obj "o2" (o2 :> List_fns.obj); > print_endline ("calling o1: " ^ (List_fns.get_obj "o1")#hello); > print_endline ("calling o2: " ^ (List_fns.get_obj "o2")#hello) > -------------------------------------------------------------------- > > -- > Caml-list mailing list. Subscription management and archives: > https://sympa.inria.fr/sympa/arc/caml-list > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners > Bug reports: http://caml.inria.fr/bin/caml-bugs > --001a11c17f62ade479054922081d Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
Hi Richard,

Note that you can still wri= te < hello: string; .. > in the signature of
register_obj a= nd do the cast before adding your object to the list:

<= div>=C2=A0 let register_obj name obj =3D objs :=3D (name, (obj :> < h= ello: string >)) :: !objs

This way you do not n= eed to downcast at each call-site of register_obj.

Cheers,
Nicolas



On Wed, Feb 22, 2017 at = 6:36 PM, Richard W.M. Jones <rich@annexia.org> wrote:
On Wed, Feb 22, 2017 at 12:09:3= 7PM -0500, Gabriel Scherer wrote:
> The signature you demand is actually incorrect, because the return
> type of get_obj is to permissive: it claims that it can return *any* > ('a obj), so you could register an object with just the hello meth= od
> and get back an object with more methods.
>
> On the other hand, if you use the monomorphic < hello : string >= type
> in the interface, then it is type-correct and the implementation you > provide is accepted. It is explicit in the fact that you can't rec= over
> the extra methods of the object once they have been stored in this
> lowest-common-denominator container.

Ah, I see.

Modifying the code to use the monomorphic type works, but I have to
downcast the objects to the right type.=C2=A0 For reference, the working
code is below.

Thanks,

Rich.

list_fns.mli --------------------------------------------------------<= br> type obj =3D < hello : string >
val register_obj : string -> obj -> unit
val get_obj : string -> obj

list_fn= s.ml ---------------------------------------------------------
type obj =3D < hello : string >
let objs =3D ref []
let register_obj name obj =3D objs :=3D (name, obj) :: !objs
let get_obj name =3D List.assoc name !objs

test= .ml -------------------------------------------------------------
class foo =3D object
=C2=A0 method hello =3D "hello from foo"
=C2=A0 method goodbye () =3D print_endline "goodbye"
end

class bar =3D object
=C2=A0 method hello =3D "hello from bar"
end

let () =3D
=C2=A0 let o1 =3D new foo in
=C2=A0 let o2 =3D new bar in
=C2=A0 List_fns.register_obj "o1" (o1 :> List_fns.obj);
=C2=A0 List_fns.register_obj "o2" (o2 :> List_fns.obj);
=C2=A0 print_endline ("calling o1: " ^ (List_fns.get_obj "o1= ")#hello);
=C2=A0 print_endline ("calling o2: " ^ (List_fns.get_obj "o2= ")#hello)
-----------------------------------------------------------------= ---

--
Caml-list mailing list.=C2=A0 Subscription management and archives:
https://sympa.inria.fr/sympa/arc/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs

--001a11c17f62ade479054922081d--