What is the difference between records and tuples in OCaml

Is there a difference between records and tuples that is not just a syntactic difference?

Is there a difference in performance?

Is the implementation the same for tuples and records?

Do you have examples of what can be done with tuples, but not with records (and vice versa)?

+4
source share
2 answers

Fields in floating point fields or arrays are stored in an uncluttered state, while such optimization does not apply to tuples. If you store a lot of floats and just float, it's important to use records - and you can get by splitting a mixed float / other data structure so that you have an internal floating-point record only.

Other differences are at the level level and are already described by Andreas - records are generative, and tuples already exist and have structural semantics. If you need structural records with polymorphic accessories, you can use object types.

+8
source

The modular syntax is almost the same. The main semantic difference is that tuples are structural types, and records are nominal. This implies, for example, that records can be recursive, while tuples cannot (at least without the -rectypes option):

type t = {a : int, b : unit -> t} (* fine *) type u = int * (unit -> u) (* error *) 

In addition, records can have mutable fields, tuples cannot.

FWIW, in the SML OCaml language, tuples are records. That is, in SML (a, b, c) only syntactic sugar is used for {1 = a, 2 = b, 3 = c}, and records are also structural types.

+12
source

Source: https://habr.com/ru/post/1411105/


All Articles