From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@sympa.inria.fr Delivered-To: caml-list@sympa.inria.fr Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by sympa.inria.fr (Postfix) with ESMTPS id 69F7B7EE09 for ; Fri, 26 Oct 2012 23:31:05 +0200 (CEST) Received-SPF: None (mail1-smtp-roc.national.inria.fr: no sender authenticity information available from domain of anil@recoil.org) identity=pra; client-ip=89.16.177.154; receiver=mail1-smtp-roc.national.inria.fr; envelope-from="anil@recoil.org"; x-sender="anil@recoil.org"; x-conformance=sidf_compatible Received-SPF: None (mail1-smtp-roc.national.inria.fr: no sender authenticity information available from domain of anil@recoil.org) identity=mailfrom; client-ip=89.16.177.154; receiver=mail1-smtp-roc.national.inria.fr; envelope-from="anil@recoil.org"; x-sender="anil@recoil.org"; x-conformance=sidf_compatible Received-SPF: None (mail1-smtp-roc.national.inria.fr: no sender authenticity information available from domain of postmaster@dark.recoil.org) identity=helo; client-ip=89.16.177.154; receiver=mail1-smtp-roc.national.inria.fr; envelope-from="anil@recoil.org"; x-sender="postmaster@dark.recoil.org"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AnACAHkAi1BZELGagWdsb2JhbABEsD6SOwEBFiYngh4BAQQBJxM0CwULCyUhRRIGExKFfoFkAwkGBAeyaQOJXotxAQmGA2EDiFmNGoEXkjaBRA X-IronPort-AV: E=Sophos;i="4.80,656,1344204000"; d="scan'208";a="179134778" Received: from recoil.dh.bytemark.co.uk (HELO dark.recoil.org) ([89.16.177.154]) by mail1-smtp-roc.national.inria.fr with SMTP; 26 Oct 2012 23:31:04 +0200 Received: (qmail 8527 invoked by uid 634); 26 Oct 2012 21:31:03 -0000 X-Spam-Level: * X-Spam-Check-By: dark.recoil.org Received: from visnet-57.csl.sri.com (HELO visnet-57.csl.sri.com) (130.107.98.57) (smtp-auth username remote@recoil.org, mechanism cram-md5) by dark.recoil.org (qpsmtpd/0.84) with ESMTPA; Fri, 26 Oct 2012 22:31:03 +0100 Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Mac OS X Mail 6.1 \(1498\)) From: Anil Madhavapeddy In-Reply-To: <1351274501735@names.co.uk> Date: Fri, 26 Oct 2012 14:31:00 -0700 Cc: Content-Transfer-Encoding: 7bit Message-Id: References: <1351274501735@names.co.uk> To: "Mark" X-Mailer: Apple Mail (2.1498) X-Virus-Checked: Checked by ClamAV on dark.recoil.org Subject: Re: [Caml-list] accessing the type of a polymorphic parameter 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" 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