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: Maxence Guesdon <max@s...>
Subject: Re: Same label in different types, how do people solve this?
Mattias Waldau a écrit :
> 
> 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?

In your example, and without objects, I would have chosen explicit names
for labels, i.e. :

type point_3d = {
     p3d_x:float;
     p3d_y:float;
     p3d_z:float;
   }
 
type point_2d = {
     p2d_x:float;
     p2d_y:float;
   }

It sure is kind of a pain to write, but it is much more easy to
understand when you read it. With
explicit labels, you "see" the type of toto in toto.p3d_x. I think not
allowing to have the same label
in different types is a great way to force developers to write cleaner
code ;-)


----------------------
Maxence Guesdon
http://maxence.guesdon.free.fr