caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] polymorphic type constructor deconstructor
@ 2003-06-06 21:30 Jeffrey J. Cook
  2003-06-06 22:37 ` Jeffrey J. Cook
  2003-06-10 15:28 ` Damien Doligez
  0 siblings, 2 replies; 5+ messages in thread
From: Jeffrey J. Cook @ 2003-06-06 21:30 UTC (permalink / raw)
  To: caml-list

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


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2003-06-10 15:29 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-06-06 21:30 [Caml-list] polymorphic type constructor deconstructor Jeffrey J. Cook
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

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).