Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[Caml-list] Records with same structure in OCaml
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2002-04-07 (15:48)
From: tim@f...
Subject: Re: [Caml-list] Records with same structure in OCaml
>type complex = { re: float; im: float };
>type foo     = { re: float; im: float };
>let x = { re =2.0; im = 55.0 };
>The x here has type foo.
>How can I choose type complex?

>From experiments with the system it seems that declaring foo binds new
meanings to the .re and .im field operators.  Before the declaration
they fetched fields from complex numbers, and after the declaration
they fetch fields from "foo".  After doing your code above, the
following happens:

># ;;
>- : float = 2
># type complex = { re: float; im: float };;
>type complex = { re : float; im : float; } 
>Toplevel input:
>  ^
>This expression has type foo but is here used with type complex

See how the redeclaration of complex changes the meaning of ".re" so
you can't get "" any more, if x is a foo.

If the field operators were ambiguous, that would make type inference
much more difficult, so I'm not surprised by this.  If we have the declaration

   let z y =;;

then we can give z the type complex -> float, or foo -> float, but
there's no way within OCaml's language of types to express
(either a complex or foo) -> float.  Thus type inference needs a
commitment that .re either takes a complex or a foo, but not either.

Tim Freeman
To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: