From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id AAA17893; Sat, 7 Jun 2003 00:37:42 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id AAA18061 for ; Sat, 7 Jun 2003 00:37:40 +0200 (MET DST) Received: from innsmouth.crhc.uiuc.edu (innsmouth.crhc.uiuc.edu [130.126.141.158]) by nez-perce.inria.fr (8.11.1/8.11.1) with ESMTP id h56MbdT08648 for ; Sat, 7 Jun 2003 00:37:40 +0200 (MET DST) Received: (from jjcook@localhost) by innsmouth.crhc.uiuc.edu (8.11.6/8.11.6) id h56MbdL28177 for caml-list@inria.fr; Fri, 6 Jun 2003 17:37:39 -0500 Date: Fri, 6 Jun 2003 17:37:39 -0500 From: "Jeffrey J. Cook" To: caml-list@inria.fr Subject: Re: [Caml-list] polymorphic type constructor deconstructor Message-ID: <20030606223739.GA28169@crhc.uiuc.edu> References: <20030606213021.GE27432@crhc.uiuc.edu> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20030606213021.GE27432@crhc.uiuc.edu> User-Agent: Mutt/1.4i X-Spam: no; 0.00; caml-list:01 verbose:01 foo:01 passing:01 populated:01 deconstruct:01 dummy:01 formed:98 int:01 polymorphic:01 constructors:01 match:02 exception:02 constructor:03 argument:03 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk 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