caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Mikhail Mandrykin <mandrykin@ispras.ru>
To: caml-list@inria.fr, Ivan Gotovchits <ivg@ieee.org>
Cc: "Richard W.M. Jones" <rich@annexia.org>,
	Damien Guichard <alphablock@orange.fr>
Subject: Re: [Caml-list] List of structurally typed objects
Date: Wed, 22 Feb 2017 22:04:16 +0300	[thread overview]
Message-ID: <2432820.aHjfWRbG44@molnar> (raw)
In-Reply-To: <CALdWJ+yo_tmH6K5QyX-JT_jvhPKUbSHR1+zBZR+P2Zi9k9ToCQ@mail.gmail.com>

On среда, 22 февраля 2017 г. 13:08:23 MSK Ivan Gotovchits wrote:
> It is possible to generalize this approach using private row types.

If at least a limited support for dynamic object typing is needed, it's also 
possible to construct a heterogeneous container of objects using several 
advanced features like first-class modules and GADT, but even in this case the 
exact type of the object should match precisely when extracted from the list 
or at least a finite (and small) number of possibilities for that type should 
be known. It's, however, still possible to upcast the object to some supertype 
upon addition and later extract and use it as the object of that supertype. An 
example: https://github.com/schrodibear/ocaml_tricks/blob/master/object_hlist/
obj_list.ml

Mikhail

> 
> list_fns.mli:
> 
> module Registry(Obj : sig
>     type t = private < .. >
>   end) : sig
>   val register_obj : string -> Obj.t -> unit
>   val get_obj : string -> Obj.t
> end
> 
> 
> list_fns.ml:
> 
> module Registry(Obj : sig
>     type t = private < .. >
>   end) = struct
> 
>   let objs = ref []
>   let register_obj name obj = objs := (name, obj) :: !objs
>   let get_obj name = List.assoc name !objs
> end
> 
> 
> 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
> 
> module Bars = List_fns.Registry(struct type t = bar end)
> 
> let () =
>   let o1 = new foo in
>   let o2 = new bar in
>   Bars.register_obj "o1" (o1 :> bar);
>   Bars.register_obj "o2" (o2 );
>   print_endline ("calling o1: " ^ (Bars.get_obj "o1")#hello);
>   print_endline ("calling o2: " ^ (Bars.get_obj "o2")#hello)
> 
> 
> 
> 
> The List_fns.Registry functor will create a monomorphic registry for the
> specified base type.
> 
> 
> Probably, this may help you :)
> 
> Best wishes,
> Ivan Gotovchits
> 
> On Wed, Feb 22, 2017 at 12:38 PM, Richard W.M. Jones <rich@annexia.org>
> 
> wrote:
> > On Wed, Feb 22, 2017 at 06:28:51PM +0100, Damien Guichard wrote:
> > > Hi Richard.
> > > 
> > > Is this the code you want ?
> > > 
> > > type 'a obj_list = 'a list
> > > constraint 'a = < hello : string; .. > as 'a
> > 
> > Perhaps.  Is it possible to write the list_fns.mli interface
> > using this?
> > 
> > Rich.
> > 
> > 
> > --
> > 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


-- 
Mikhail Mandrykin
Linux Verification Center, ISPRAS
web: http://linuxtesting.org
e-mail: mandrykin@ispras.ru

  reply	other threads:[~2017-02-22 19:04 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
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 [this message]
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=2432820.aHjfWRbG44@molnar \
    --to=mandrykin@ispras.ru \
    --cc=alphablock@orange.fr \
    --cc=caml-list@inria.fr \
    --cc=ivg@ieee.org \
    --cc=rich@annexia.org \
    /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).