From: Goswin von Brederlow <goswin-v-b@web.de>
To: caml-list@inria.fr
Subject: Re: [Caml-list] Obj.magic for polymorphic identifiers
Date: Mon, 28 Apr 2014 09:36:25 +0200 [thread overview]
Message-ID: <20140428073625.GA5776@frosties> (raw)
In-Reply-To: <CAPi0vKU-V2K_jMVRkYteNqoqsx00Dpx4r5XAYoS2r1ZtS6Kc5w@mail.gmail.com>
On Thu, Apr 24, 2014 at 06:30:24PM +0300, Dmitry Grebeniuk wrote:
> Hello.
>
> It's perfectly doable without Obj at all.
> I needed "semi-typed" values quite often, so
> decided to write reusable library "cadastr" [1]
> that handles them, along with "untyped methods",
> structural [de]composition (hence [de]serialization
> based on value structure) and more.
>
> Things you need could be written like:
>
> $ ocaml
> Objective Caml version 3.12.1
>
> # #use "topfind";;
> [...]
> # #require "cadastr";;
> [...]
> # open Cdt;;
> # let hlist = [ubox ti_int 123; ubox ti_string "qwe"; ubox ti_bool true];;
> val hlist : Cdt.ubox list =
> [{ub_store = <fun>; ub_uti = <obj>}; {ub_store = <fun>; ub_uti = <obj>};
> {ub_store = <fun>; ub_uti = <obj>}]
How do you encode a
type foo = Foo | Bar
Someone has to implement a ti_foo for it.
> # let dump ub =
> let uti = ub.ub_uti in
> if uti == (ti_string :> uti) then uget_exn ti_string ub else
> if uti == (ti_int :> uti) then string_of_int (uget_exn ti_int ub) else
> if uti == (ti_bool :> uti) then string_of_bool (uget_exn ti_bool ub) else
> "<unknown>";;
Here you have to match every possible type, which doesn't scale. Or
you limit yourself to structure, which is unsound for retrieving the
original type.
> val dump : Cdt.ubox -> string = <fun>
> # List.iter (fun ub -> print_endline (dump ub)) hlist;;
> 123
> qwe
> true
> - : unit = ()
> #
So you can print the values. But how to you get them back?
Where is your
val unbox : type a . uti -> Cdt.ubox -> a
(which isn't well typed like that)
> As for "methods" I 've mentioned above,
>
> # List.iter (fun ub ->
> let ushow = get_meth_untyped "show" ub.ub_uti in
> let ures = u_app ushow ub in
> print_endline (uget_exn ti_string ures)
> ) hlist;;
> 123
> "qwe"
> True
> - : unit = ()
> #
>
> However there is some runtime cost on such
> "untyped" values.
>
>
> [1] https://bitbucket.org/gds/cadastr , but it doesn't
> compile with OCaml >= 4. It can be fixed;
> I'll do it if someone will want to use it.
MfG
Goswin
next prev parent reply other threads:[~2014-04-28 7:36 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-04-22 8:03 Romain Bardou
2014-04-22 8:31 ` Jeremie Dimino
2014-04-24 14:28 ` Goswin von Brederlow
2014-05-04 23:46 ` Leo White
2014-04-24 15:30 ` Dmitry Grebeniuk
2014-04-28 7:36 ` Goswin von Brederlow [this message]
2014-04-28 8:13 ` Dmitry Grebeniuk
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=20140428073625.GA5776@frosties \
--to=goswin-v-b@web.de \
--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