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: Brian Rogoff <bpr@b...>
Subject: Re: Same label in different types, how do people solve this?
Mattias Waldau wrote : 
> In Ocaml, you cannot have the same label in different types, see the
example
> below where point_2d hides point_3d.

Troublemaker :-). There is a thread on this topic on comp.lang.functional. 
Please, FAQ maintainer, this is very definitely FAQ material!

> 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.} ???

John Skaller and Jacques Garrigue answered this.

Maxence Guesdon wrote: 

> 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

I very strongly disagree. If I didn't disagree, I'd probably also prefer
to annotate all of my functions in place with their types. Same principle, a
pain to write but somewhat easier to read. I don't agree with the principle
that more annotations means easier readability, and I've written a fair enough 
amount of Ada to believe that my opinion is not without some merit. I think 
annotating each field is way too verbose, even for the reader. My opinion
of course, feel free to differ. 

I do agree with the general Ada principle that you shouldn't be concerned 
with the writer of the code, but with the reader/maintainer. I guess one 
counterargument though is that code which is never written won't be
maintained anyways ;-).

Jacques Garrigue wrote:
> 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).

I prefer the module solution. 

> The last solution might be nicer if we had a
>     let open Module in
> construct (would require 10 lines in the compiler)

Yes, I would like something like this! There is a workaround, in which you
can use a local module and do your "open" in that. 

let module Top = struct open Module let f () = ... end in Top.f () 

but I think a cleaner syntax might be nice and I'm reluctant to use P4 in 
work projects until its status is clearer.

For this problem, you're still hosed if you want to use 2d and 3d points
in the same module, and don't want distinct labels. 

> Polymorphic variants cannot help, but if you do not care about
> efficiency you can use objects:

Maybe the efficient compilation of OO code deserves more attention,
especially for this case where we use classes just to get polymorphic
records. 

> > 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.} ???

> 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...

Yes, still pretty weak. My gut feeling is that putting the records in
separate modules and having a local open construct would be better, if
developers are dead set on preserving the principal type property. But,
maybe in combination with the local open this is "good enough"?

Pierre Weis wrote
> I would suggest another syntactic notation to specify the type to
> which a label belongs: label@@type.
> 
> {x@@p2d = 0; y = 5}
> r.x@@p2d

I don't want to start a syntax war, but I think Ocaml is already suffering
a bit from too much added syntax and this doesn't look good to me. Jacques' 
reuse of ":" (I know, it applies to the whole record and not a label) is 
easier on my eyes, which already associate ":" with typing. 

-- Brian