caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Damien Guichard <alphablock@orange.fr>
To: caml-list@inria.fr
Subject: Re: [Caml-list] List of structurally typed objects
Date: Wed, 22 Feb 2017 18:28:51 +0100	[thread overview]
Message-ID: <61e8bf6f-4abc-26e7-7b0b-ce935faaf8b8@orange.fr> (raw)
In-Reply-To: <20170222170101.GE28111@annexia.org>


Hi Richard.

Is this the code you want ?

type 'a obj_list = 'a list
constraint 'a = < hello : string; .. > as 'a

hope it helps,

Damien Guichard (SpiceGuid)

Richard W.M. Jones a écrit :
> Is it possible to construct a list of structurally typed ("duck
> typed") objects?
>
> The code below seems as if superficially it should work, at least as
> far as I understand the OCaml memory model and how objects work
> internally.  However the implementation doesn't match the interface.
>
> ------ list_fns.mli ----------------------------
> type 'a obj = < hello : string; .. > as 'a
> val register_obj : string -> 'a obj -> unit
> val get_obj : string -> 'a obj
> ------------------------------------------------
>
> ------ list_fns.ml -----------------------------
> type 'a obj = < hello : string; .. > as 'a
> let objs = ref []
> let register_obj name obj = objs := (name, obj) :: !objs
> let get_obj name = List.assoc name !objs
> ------------------------------------------------
>
> $ ocamlc -c list_fns.mli
> $ ocamlc -c list_fns.ml
> File "list_fns.ml", line 1:
> Error: The implementation list_fns.ml
>         does not match the interface list_fns.cmi:
>         Values do not match:
>           val register_obj : string -> '_a -> unit
>         is not included in
>           val register_obj : string -> < hello : string; .. > obj -> unit
>         File "list_fns.ml", line 3, characters 4-16: Actual declaration
>
> I'm guessing that "constraint" should be used here, but how?
>
> Rich.
>


  parent reply	other threads:[~2017-02-22 17:28 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 [this message]
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=61e8bf6f-4abc-26e7-7b0b-ce935faaf8b8@orange.fr \
    --to=alphablock@orange.fr \
    --cc=caml-list@inria.fr \
    /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).