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: -- (:)
From: Jeffrey J. Cook <jjcook@u...>
Subject: Re: [Caml-list] polymorphic type constructor deconstructor
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

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


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

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