From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id QAA22215 for caml-red; Mon, 12 Jun 2000 16:12:41 +0200 (MET DST) Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id KAA30981 for ; Mon, 12 Jun 2000 10:28:53 +0200 (MET DST) Received: from kurims.kurims.kyoto-u.ac.jp (kurims.kurims.kyoto-u.ac.jp [130.54.16.1]) by concorde.inria.fr (8.10.0/8.10.0) with ESMTP id e5C8SpD22908 for ; Mon, 12 Jun 2000 10:28:52 +0200 (MET DST) Received: from localhost (sansho.kurims.kyoto-u.ac.jp [130.54.16.90]) by kurims.kurims.kyoto-u.ac.jp (8.9.3/3.7W) with ESMTP id RAA24664; Mon, 12 Jun 2000 17:28:05 +0900 (JST) To: David.Chemouil@enseeiht.fr Cc: caml-list@inria.fr Subject: Re: polymorphic variants (2) + magic In-Reply-To: Your message of "Fri, 09 Jun 2000 11:15:58 +0200" <3940B5CE.57FEE3E4@enseeiht.fr> References: <3940B5CE.57FEE3E4@enseeiht.fr> X-Mailer: Mew version 1.93 on Emacs 20.5 / Mule 4.0 (HANANOEN) Mime-Version: 1.0 Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit Message-Id: <20000612172754Q.garrigue@kurims.kyoto-u.ac.jp> Date: Mon, 12 Jun 2000 17:27:54 +0900 From: Jacques Garrigue X-Dispatcher: imput version 980905(IM100) Sender: weis From: David Chemouil Subject: polymorphic variants (2) + magic Date: Fri, 09 Jun 2000 11:15:58 +0200 > Considering my two previous posts ("polymorphic variants" and > "Obj.magic"), I'd like to show how I had to merge them to have my > application work. > > Indeed, I don't know yet completely the type 'argument', but I already > know some of its constructors. Then I use polymorphic constructors when > I need them and, when I'm able to do it, I will generate the 'argument' > type. You don't necessarily have to use polymorphic variants for that. In fact the goal of polymorphic variant is somewhat othogonal to your problem. With standard sums, you can write type 'a message = Server_message1 | Server_message2 | Server_message3 | Application_message of 'a Then you can write all your server functions handling only server messages, and returning application messages to the application. Moreover, if your server is going to run as an indepedent program, communicating with applications using input_value/output_value, then you probably want to make application messages abstract. types app_msg type server_message = app_msg message This way you are sure your server will not fiddle with application messages. It can receive or send them, but not read their contents. Polymorphic variants will only be useful if you have a more complex hierarchy of messages, and want for instance to share some messages (but not all) between different applications; but it will do nothing for solving your first problem. > The problem is that there are already functions which manipulate the > 'argument' type. So, if I try to use them on an already known > polymorphic constructor, it doesn't work, because the constructor is not > known to belong to 'argument', as 'argument' isn't defined. With the structure I suggest above, your functions will be polymorphic in the type of application messages, so this problem does not occur. > As the example shows, the only solution for me is to use Obj.magic. Then > I'm sure everything will be well-typed. But is it completely correct to > do like this? Certainly not correct. You should _never_ use Obj.magic. When you use it things are not well-typed, they are just untyped, and you can say hello to segmentation faults. This is only intended to do on the Caml side things that otherwise you would have to do anyway on the C-side. So, basically the only case in which you can use Obj.magic is when you have _yourself_ implemented a C interface, and want to do some casts on C-values. Never use it on ML values. Jacques --------------------------------------------------------------------------- Jacques Garrigue Kyoto University garrigue at kurims.kyoto-u.ac.jp JG