Version française
Home     About     Download     Resources     Contact us    
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: -- (:)
From: Gerd Stolpmann <gerd@g...>
Subject: Re: Same label in different types, how do people solve this?
On Wed, 06 Dec 2000, Mattias Waldau wrote:
>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?

The simplest way is to use different labels. 

Another solution is to wrap the type definition into modules:

module P3d = struct
type point_3d = {
    x:float;
    y:float;
    z:float;
  }
end

module P2d = struct
type point_2d = {
    x:float;
    y:float;
  }
end

Now you can prefix the labels with the module identifiers:

{ P3d.x = 10.; P3d.y = 20.; P3d.z = 30. }, or
{ P2d.x = 10.; P2d.y = 20.}

You can open the module whose type is used more frequently:

open P2d
{ x = 10.; y = 20.; }

Polymorphic variants are union types and not product types, so they do not help
very much.

Gerd
-- 
----------------------------------------------------------------------------
Gerd Stolpmann      Telefon: +49 6151 997705 (privat)
Viktoriastr. 100             
64293 Darmstadt     EMail:   gerd@gerd-stolpmann.de
Germany                     
----------------------------------------------------------------------------