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: [Caml-list] polymorphic type constructor deconstructor
Date: Fri, 6 Jun 2003 16:30:21 -0500	[thread overview]
Message-ID: <20030606213021.GE27432@crhc.uiuc.edu> (raw)

Is there any way to polymorphically deconstruct a type constructor, thus
not requiring a pattern match statement?

I am using socket communication between processes and would like to utilize
type checking to ensure I always have well-formed messages.

Right now I'm doing something like (many more cases of course):

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

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

and am using input_value and output_value with sockets to send/receive
messages.  My approach right now requires me to do something similar to:

let request = REQ_do_this 5 in
let reply = Socket.client "mysocket" request in
let success =
  match reply with
  | REPLY_do_this x -> x
  | _ -> failwith "this case shouldn't happen and is annoying"

however, I would like to do something more like:

let request = REQ_do_this(5) in
let success = deconstruct (REPLY_do_this(socket_send request)) in

where the polymorphic 'deconstruct' declaration is:

val deconstruct : 'a -> 'b

Where 'a is a type constructed type such as type foo = Bar of int * int
and thus 'b is of type (int * int)

My complication is that I have multiple sets of request and reply types
in different modules which I would like to deconstruct without duplicating
the function, not to meantion not manually enumerating all types and
their straightforward deconstruction.


Any ideas on how to do or rework to avoid this?

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 21:30 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-06-06 21:30 Jeffrey J. Cook [this message]
2003-06-06 22:37 ` Jeffrey J. Cook
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=20030606213021.GE27432@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).