From: Anil Madhavapeddy <anil@recoil.org>
To: "Mark" <mark@proof-technologies.com>
Cc: <caml-list@inria.fr>
Subject: Re: [Caml-list] accessing the type of a polymorphic parameter
Date: Fri, 26 Oct 2012 14:31:00 -0700 [thread overview]
Message-ID: <DD556553-8D07-467B-B8BB-E4F4281AC1F9@recoil.org> (raw)
In-Reply-To: <1351274501735@names.co.uk>
You could use the Dyntype extension for this, if you don't mind a spot
of Camlp4 being involved: https://github.com/mirage/dyntype
Toplevel:
$ #require "dyntype.syntax" ;;
$ type t = { foo: int; bar: string } with type_of;;
type t = { foo : int; bar : string; }
val type_of_t : Dyntype.Type.t = Dyntype.Type.Ext ("t",
Dyntype.Type.Dict (`R, [("foo", `RO, Dyntype.Type.Int (Some 63));
("bar", `RO, Dyntype.Type.String)]))
...and you can collect and inspect the dynamic types in other code.
This won't work with polymorphic types, but you can probably rephrase
your problem to it work with this approach.
Note that this suffers from the usual drawbacks of camlp4-based
interpretation of type declarations, but it works fairly well "in the
wild" and with an existing, unmodified OCaml toolchain.
-anil
On 26 Oct 2012, at 11:01, "Mark" <mark@proof-technologies.com> wrote:
> Hello.
>
> Is it possible in OCaml for a polymorphic function to somehow have
> access to the concrete type of an actual parameter used in a call of
> the function?
>
> For example, is there some way of writing a polymorphic function 'foo'
> list this:
>
> let foo x =
> match (type_of x) with
> Int -> ....
> | String -> ....
>
> where 'type_of' is some function that returns a representation of the
> concrete type of its argument.
>
> I want this because I want to implement an ad-hoc polymorphic
> function. I know this is outside the bounds of "normal" OCaml, but is
> there some nasty part of the language like Obj.magic that I could use
> to achieve this?
>
> Mark.
>
> --
> 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
next prev parent reply other threads:[~2012-10-26 21:31 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-10-26 18:01 "Mark"
2012-10-26 19:23 ` Gabriel Scherer
2012-10-26 21:31 ` Anil Madhavapeddy [this message]
2012-10-26 19:39 "Mark"
2012-10-26 19:58 ` Gabriel Scherer
2012-10-26 20:01 ` Gabriel Scherer
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=DD556553-8D07-467B-B8BB-E4F4281AC1F9@recoil.org \
--to=anil@recoil.org \
--cc=caml-list@inria.fr \
--cc=mark@proof-technologies.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