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 KAA12005 for caml-red; Fri, 8 Dec 2000 10:15:57 +0100 (MET) 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 CAA28582 for ; Fri, 8 Dec 2000 02:22:53 +0100 (MET) Received: from kurims.kurims.kyoto-u.ac.jp (kurims.kurims.kyoto-u.ac.jp [130.54.16.1]) by nez-perce.inria.fr (8.11.1/8.10.0) with ESMTP id eB81MpL05035 for ; Fri, 8 Dec 2000 02:22:52 +0100 (MET) Received: from localhost (mikan.kurims.kyoto-u.ac.jp [130.54.16.202]) by kurims.kurims.kyoto-u.ac.jp (8.9.3/3.7W) with ESMTP id KAA03592; Fri, 8 Dec 2000 10:22:29 +0900 (JST) To: mattias.waldau@abc.se Cc: caml-list@inria.fr Subject: Re: Same label in different types, how do people solve this? In-Reply-To: References: X-Mailer: Mew version 1.94.2 on Emacs 20.7 / Mule 4.0 (HANANOEN) Mime-Version: 1.0 Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit Message-Id: <20001208102229N.garrigue@kurims.kyoto-u.ac.jp> Date: Fri, 08 Dec 2000 10:22:29 +0900 From: Jacques Garrigue X-Dispatcher: imput version 20000228(IM140) Sender: weis@pauillac.inria.fr From: "Mattias Waldau" > In Ocaml, you cannot have the same label in different types, see the example > below where > point_2d hides point_3d. > > How do people normally code around this restriction? One solution is using > objects, but what other solutions are there? Can 'Polymorphic variants' > solve this? There are various ways. The most common is to differentiate label names (i.e. use x_3d and x_2d instead of x). Another more subtle one is to put the definitions in deifferent modules (Point2d and Point3d) so that you can specify exactly which label you want (Point2d.x or Point3d.x). The last solution might be nicer if we had a let open Module in construct (would require 10 lines in the compiler) Polymorphic variants cannot help, but if you do not care about efficiency you can use objects: class point_3d ~x ~y ~z = object method x : float = x method y : float = y method z : float = z end class point_2d ~x ~y = object method x : float = x method y : float = y end Note that objects provide you more than overloading: you can define truly polymorphic functions, working on both point_3d and point_2d, and you can even coerce a point_3d to a point_2d. > Also, I am a bit curious why it doesn't help to type explicitely, i.e. to > write > let x:point_3d={x=10.;y=20.;z=30.} ??? Because this is not the way it works. Labels are defined as are values or constructors, this is only afterwards that the compiler checks that they all belong to the same type. That is, the type cannot influence the choice between x(point_3d) and x(point_2d), only the order of definition can. There has been already various proposals for allowing types to be taken into account when typing record labels. The difficulty is that as soon as you make it into something useful, you loose the principal type property, and that some theoreticians working on ocaml wouldn't like it. A very restrictive approach would still work: use type annotations, but only when they are directly on the creation/access/modification construct. That is ({x=1.;y=1.;z=1.}:point_3d) let p : point_3d = {x=1.;y=1.;z=1.} (p : point_3d).x {p : point_3d with x = 2.} Here, there is no ambiguity at all. But this is very weak: let p : point_3d = {x=1.;y=1.;z=1.} in p.x +. p.y +. p.z would still be illegal... Jacques --------------------------------------------------------------------------- Jacques Garrigue Kyoto University garrigue at kurims.kyoto-u.ac.jp JG