caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Records with same structure in OCaml
@ 2002-04-06 17:52 Oliver Bandel
  2002-04-07 15:38 ` Tim Freeman
  2002-04-07 16:43 ` Brian Rogoff
  0 siblings, 2 replies; 3+ messages in thread
From: Oliver Bandel @ 2002-04-06 17:52 UTC (permalink / raw)
  To: caml-list

Hello,


when I use two records with the same structure,
how can they be distinguished? And how can
the correct type be choosed?


Example:

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?

Doesn't that yield a mess, when calling
functions with that values?

I tried some things on the toplevel,
and it seems messy to me.


How will that be handled in Ocaml?
Is it not good to have two types with
same structure?

Ciao,
   Oliver

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] Records with same structure in OCaml
  2002-04-06 17:52 [Caml-list] Records with same structure in OCaml Oliver Bandel
@ 2002-04-07 15:38 ` Tim Freeman
  2002-04-07 16:43 ` Brian Rogoff
  1 sibling, 0 replies; 3+ messages in thread
From: Tim Freeman @ 2002-04-07 15:38 UTC (permalink / raw)
  To: oliver; +Cc: caml-list

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

># x.re ;;
>- : float = 2
># type complex = { re: float; im: float };;
>type complex = { re : float; im : float; } 
># x.re;;
>Toplevel input:
># x.re;;
>  ^
>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 "x.re" 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 = y.re;;

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       
tim@fungible.com
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] Records with same structure in OCaml
  2002-04-06 17:52 [Caml-list] Records with same structure in OCaml Oliver Bandel
  2002-04-07 15:38 ` Tim Freeman
@ 2002-04-07 16:43 ` Brian Rogoff
  1 sibling, 0 replies; 3+ messages in thread
From: Brian Rogoff @ 2002-04-07 16:43 UTC (permalink / raw)
  To: Oliver Bandel; +Cc: caml-list

On Sat, 6 Apr 2002, Oliver Bandel wrote:
> Hello,
>
>
> when I use two records with the same structure,
> how can they be distinguished? And how can
> the correct type be choosed?

This question comes up frequently enough to be a FAQ entry

http://caml.inria.fr/FAQ/FAQ_EXPERT-eng.html#labels_surcharge

and the most frequent answer is "Don't do that!", meaning either
disambiguate the field names if the records are defined in the same module

type complex = { complex_re: float; complex_im: float };
type foo     = { foo_re: float; foo_im: float };

or put each definition in its own module and use the fully qualified name
if you must mix them. You can also use classes, but if you're a
performance junkie you should realize that you give up a lot of
performance, especially in the example you use, complex numbers. You also
lose pattern matching (and gain some polymorphism) is you use the class
system.

-- Brian
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2002-04-07 21:24 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-04-06 17:52 [Caml-list] Records with same structure in OCaml Oliver Bandel
2002-04-07 15:38 ` Tim Freeman
2002-04-07 16:43 ` Brian Rogoff

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).