caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Richard W.M. Jones" <rich@annexia.org>
To: Gabriel Scherer <gabriel.scherer@gmail.com>
Cc: caml users <caml-list@inria.fr>
Subject: Re: [Caml-list] List of structurally typed objects
Date: Wed, 22 Feb 2017 17:36:45 +0000	[thread overview]
Message-ID: <20170222173645.GF28111@annexia.org> (raw)
In-Reply-To: <CAPFanBEWRGWhZZ6wNMGfsXqe0SewZHa3BS7DbTkCrgSZzfvpNw@mail.gmail.com>

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)
--------------------------------------------------------------------

  reply	other threads:[~2017-02-22 17:36 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-02-22 17:01 Richard W.M. Jones
2017-02-22 17:09 ` Gabriel Scherer
2017-02-22 17:36   ` Richard W.M. Jones [this message]
2017-02-22 17:43     ` Nicolás Ojeda Bär
2017-02-22 17:53       ` Richard W.M. Jones
2017-02-22 17:28 ` Damien Guichard
2017-02-22 17:38   ` Richard W.M. Jones
2017-02-22 18:08     ` Ivan Gotovchits
2017-02-22 19:04       ` Mikhail Mandrykin
2017-02-22 19:21         ` [Caml-list] Warning for unused variables Aymeric Fromherz
2017-02-22 19:43           ` Gabriel Scherer
2017-02-22 19:59             ` Aymeric Fromherz
2017-02-22 20:01               ` Gabriel Scherer
2017-02-22 20:17                 ` Aymeric Fromherz

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=20170222173645.GF28111@annexia.org \
    --to=rich@annexia.org \
    --cc=caml-list@inria.fr \
    --cc=gabriel.scherer@gmail.com \
    /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).