Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[Caml-list] polymorphic type constructor deconstructor
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2003-06-06 (21:30)
From: Jeffrey J. Cook <jjcook@u...>
Subject: [Caml-list] polymorphic type constructor deconstructor
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?


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

To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: