From: Dmitry Grebeniuk <gdsfh1@gmail.com>
To: Romain Bardou <caml-list@inria.fr>
Subject: Re: [Caml-list] Obj.magic for polymorphic identifiers
Date: Thu, 24 Apr 2014 18:30:24 +0300 [thread overview]
Message-ID: <CAPi0vKU-V2K_jMVRkYteNqoqsx00Dpx4r5XAYoS2r1ZtS6Kc5w@mail.gmail.com> (raw)
In-Reply-To: <5356225B.1090305@cryptosense.com>
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>}]
# 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>";;
val dump : Cdt.ubox -> string = <fun>
# List.iter (fun ub -> print_endline (dump ub)) hlist;;
123
qwe
true
- : unit = ()
#
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.
next prev parent reply other threads:[~2014-04-24 15:30 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 [this message]
2014-04-28 7:36 ` Goswin von Brederlow
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=CAPi0vKU-V2K_jMVRkYteNqoqsx00Dpx4r5XAYoS2r1ZtS6Kc5w@mail.gmail.com \
--to=gdsfh1@gmail.com \
--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