caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Jeffrey J. Cook" <jjcook@uiuc.edu>
To: caml-list@inria.fr
Subject: Re: [Caml-list] polymorphic type constructor deconstructor
Date: Fri, 6 Jun 2003 17:37:39 -0500	[thread overview]
Message-ID: <20030606223739.GA28169@crhc.uiuc.edu> (raw)
In-Reply-To: <20030606213021.GE27432@crhc.uiuc.edu>

Hmmm, I didn't make much sense, I'll try again.

I form requests sent to a server using a type constructor of type 'request':

type request =
   REQ_do_this of int |
   REQ_do_that of int * int

which pattern matches on the request type, does some processing, and then
sends back a loosely paired 'reply':

type reply =
   RELPY_do_this of successlevel |
   REPLY_do_that of successlevel * int

Using the constructors helps to both ensure the message is well formed,
plus on the server side it lets me decide what was requested.

On the client side, I only expect a single case of type constructored value
of type 'reply', and thus needing a match statement is overly verbose.

I would like to simplify this into a function taking a type constructor
of type 'reply' and a type constructed value of type 'reply' and return
the contents of the type constructed value, such as (int * int) from
Foo of int * int. This function would internally have a match statement
and throw an exception if its not of the right type constructor.

To accomplish this, I would assume I would need a deconstructor function
that at most takes a type constructor of type 'reply' and a value of type
'reply'.  However, of course, one cannot pass a type constructor as an
argument.

The only solution, using this approach, that I can think of, is by passing
a dummy constructed value, and pattern matching it against the real
constructed value, throwing an exception if it isn't of the right constructor,
otherwise decontructing the value and returning the data within.

This approach, however, is difficult since the dummy value must be populated
with dummy data, and each case would need to be pattern matched for by hand.

Furthermore, if I have seperate 'request' and 'reply' type pairs, I need to
create this hand crafted function for each.

So, is there any way to polymorphically deconstruct a type constructed value,
which is type safe by providing either a dummy constructed value or a dummy
constructor?


Thanks.
Jeff



-- 
Jeffrey J. Cook
Graduate Student, Electrical Engineering
University of Illinois at Urbana-Champaign
jjcook@uiuc.edu

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


  reply	other threads:[~2003-06-06 22:37 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-06-06 21:30 Jeffrey J. Cook
2003-06-06 22:37 ` Jeffrey J. Cook [this message]
2003-06-07  0:09   ` John Max Skaller
2003-06-07  8:22   ` Marcin 'Qrczak' Kowalczyk
2003-06-10 15:28 ` Damien Doligez

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=20030606223739.GA28169@crhc.uiuc.edu \
    --to=jjcook@uiuc.edu \
    --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).