Version française
Home     About     Download     Resources     Contact us    
Browse thread
strange behavior with record type definition
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jacques Garrigue <garrigue@m...>
Subject: Re: [Caml-list] strange behavior with record type definition
From: Florent <florentflament@aist.enst.fr>

> Ok but with these two record types defined :
>     type t0 = { x : int ; y : int } ;;
>     type t1 = { x : int } ;;
> 
> There is no ambiguity about the following expression's type:
>     { x = 0 ; y = 0 } ;;
> Why can't the t0 type be infered ?
> 
> And with this function definition,
>     let get_y (t:t0) = t.x ;;
> I explicitly say to the compiler that t is of type t0, so why does the 
> compiler infer a t1 type when trying to get the x label of a t0 type value ?

It is not a question about it being possible or not (it is possible,
and can even be made "principal"), but whether we want it in the
language or not.
Essentially, ocaml has two kinds of records: explicitly
declared records, and objects. Of course, objects as records
are not very efficient, but in many cases this doesn't matter.
You can even find a syntax extension to do it with the same syntax as
records.
  http://www.math.nagoya-u.ac.jp/~garrigue/code/ocaml.html

Knowing this, the choice is currently to make explicit records
non-ambiguous. This way you just have to look at any of the the
labels to know exactly which type is involved. This could be changed,
but you would loose this property.  Which would be fine with me, as
long as the information is still still easy to deduce without relying
on type inference.

Jacques Garrigue