English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
Same label in different types, how do people solve this?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2000-12-08 (09:15)
From: Jacques Garrigue <garrigue@k...>
Subject: Re: Same label in different types, how do people solve this?
From: "Mattias Waldau" <mattias.waldau@abc.se>

> 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

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

class point_2d ~x ~y = object
  method x : float = x
  method y : float = y

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

A very restrictive approach would still work: use type annotations,
but only when they are directly on the creation/access/modification

That is
     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 Garrigue      Kyoto University     garrigue at kurims.kyoto-u.ac.jp
		<A HREF=http://wwwfun.kurims.kyoto-u.ac.jp/~garrigue/>JG</A>