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 mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by sympa.inria.fr (Postfix) with ESMTPS id 8F8AC7F616 for ; Wed, 22 Feb 2017 18:36:47 +0100 (CET) Authentication-Results: mail3-smtp-sop.national.inria.fr; spf=None smtp.pra=rich@annexia.org; spf=Pass smtp.mailfrom=rich@annexia.org; spf=Pass smtp.helo=postmaster@annexia.org Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of rich@annexia.org) identity=pra; client-ip=80.68.91.176; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="rich@annexia.org"; x-sender="rich@annexia.org"; x-conformance=sidf_compatible Received-SPF: Pass (mail3-smtp-sop.national.inria.fr: domain of rich@annexia.org designates 80.68.91.176 as permitted sender) identity=mailfrom; client-ip=80.68.91.176; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="rich@annexia.org"; x-sender="rich@annexia.org"; x-conformance=sidf_compatible; x-record-type="v=spf1" Received-SPF: Pass (mail3-smtp-sop.national.inria.fr: domain of postmaster@annexia.org designates 80.68.91.176 as permitted sender) identity=helo; client-ip=80.68.91.176; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="rich@annexia.org"; x-sender="postmaster@annexia.org"; x-conformance=sidf_compatible; x-record-type="v=spf1" IronPort-PHdr: =?us-ascii?q?9a23=3A+ELHBBwLm1tiZw3XCy+O+j09IxM/srCxBDY+r6Qd?= =?us-ascii?q?1O8SIJqq85mqBkHD//Il1AaPBtSGrasawLOO6ujJYi8p2d65qncMcZhBBVcuqP?= =?us-ascii?q?49uEgeOvODElDxN/XwbiY3T4xoXV5h+GynYwAOQJ6tL1LdrWev4jEMBx7xKRR6?= =?us-ascii?q?JvjvGo7Vks+7y/2+94fdbghMhDexe65+IRS5oQnMqsUanJZpJ7osxBfOvnZGYf?= =?us-ascii?q?ldy3lyJVKUkRb858Ow84Bm/i9Npf8v9NNOXLvjcaggQrNWEDopM2Yu5M32rhbD?= =?us-ascii?q?VheA5mEdUmoNjBVFBRXO4QzgUZfwtiv6sfd92DWfMMbrQ704RSiu4qF2QxLzli?= =?us-ascii?q?wJKyA2/33WisxojaJUvhShpwBkw4XJZI2ZLedycr/Bcd8fQ2dKQ8RfWDFbAo6k?= =?us-ascii?q?YYUBD/QPM/tboYnzqFQBsQaxCguiC+701j9EmmX70bEm3+k7Dw3L2hErEdIUsH?= =?us-ascii?q?TTqdX4LKkeX/y0zKbUzTXMdelW1irg44fIaBwvpuuAVq90f8rWz0kgCRjFgluN?= =?us-ascii?q?ooHiJTOV1/gCs2iA4uV6U+KvjXAoqg5woje13MssjZPJho0Mx13C6C53w541KM?= =?us-ascii?q?W7RUJne9KpEZhduzueOodrWM8uXmNltSc8x7Ybo5C0ZjIKx44ixxPHa/yIbYyI?= =?us-ascii?q?4hX7WemLJzd4gmxqeb2+hxmo60Sgzff8Vsas3FZLqCpKjMXMu2gO2hDP8MSKS/?= =?us-ascii?q?hw8l2l1DqSzQzf9+NJLEEsmareMZEhw7owlpQJsUTEGy/7gF/2jK6KeUUk4eWn?= =?us-ascii?q?8eLnYrP4qZ6TNo97kBvyMqM0msywGeg4KBQBX3CH+eSg073u5VH2T69Pjv0yi6?= =?us-ascii?q?XZtJHaJd8HpqOiGA9U0oMj6w6lADu80dQYm2MHLFNfdx6dgYjpIQKGHPetIv61?= =?us-ascii?q?mVWhlH9QzPDLJLD7SsHCJ3LZkbrlO61260NGxRAbwtVW5pYSAbYEdqHdQEj04Z?= =?us-ascii?q?biDxQ5Oha4i9mhLZM19IQYVHnFSvuDMajSmVaO4O8/IuDKY5Ua7mWuY8M57uLj?= =?us-ascii?q?2Cdq0WQWerOkiN5OMCi1?= X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: =?us-ascii?q?A0CEBABoy61Y/7BbRFBeGwEBAQMBAQEJA?= =?us-ascii?q?QEBFgEBAQMBAQEJAQEBgyZhgQmDDZsCAQEBAQEBBoEil0EihgACgwVCFQEBAQE?= =?us-ascii?q?BAQEBAQEBYSiCMyIBghsBBScTPxALGAkTEg8FKCGJexGwfTqLOAEBAQcCASWGB?= =?us-ascii?q?zuEeYgIgjEFiRKGOoYahioBhnOLIoJbjkKTJTUigQBhCD6GSUA1ihQBAQE?= X-IPAS-Result: =?us-ascii?q?A0CEBABoy61Y/7BbRFBeGwEBAQMBAQEJAQEBFgEBAQMBAQE?= =?us-ascii?q?JAQEBgyZhgQmDDZsCAQEBAQEBBoEil0EihgACgwVCFQEBAQEBAQEBAQEBYSiCM?= =?us-ascii?q?yIBghsBBScTPxALGAkTEg8FKCGJexGwfTqLOAEBAQcCASWGBzuEeYgIgjEFiRK?= =?us-ascii?q?GOoYahioBhnOLIoJbjkKTJTUigQBhCD6GSUA1ihQBAQE?= X-IronPort-AV: E=Sophos;i="5.35,195,1484002800"; d="scan'208";a="214386898" Received: from annexia.org ([80.68.91.176]) by mail3-smtp-sop.national.inria.fr with ESMTP/TLS/AES128-GCM-SHA256; 22 Feb 2017 18:36:46 +0100 Received: from rich by annexia.org with local (Exim 4.84_2) (envelope-from ) id 1cgaqL-0001uY-HC; Wed, 22 Feb 2017 17:36:45 +0000 Date: Wed, 22 Feb 2017 17:36:45 +0000 From: "Richard W.M. Jones" To: Gabriel Scherer Cc: caml users Message-ID: <20170222173645.GF28111@annexia.org> References: <20170222170101.GE28111@annexia.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.23 (2014-03-12) Subject: Re: [Caml-list] List of structurally typed objects 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) --------------------------------------------------------------------