Goswin von Brederlow <goswin-...@web.de> writes: > Hi, > > consider the code below that counts how often a value is printed. > > The reason why this code works is that the memory layout of a > variant with arguments is identical to a record with the same > types. The only difference is that a variant sets the tag of the memory > block to reflect which constructor it is (here Bar = 0, Baz = 1). > > But the code is fragile. It requires the use of Obj.magic and duplicates > the definitions of bar and baz (once as variant and once as record). If > the type of foo is changed but not the records then bad things will > happen. > > There are ways to do this without Obj.magic: > > type foo = Bar of bar_record | Baz of baz_record > type foo = Bar of int * int ref | Baz of float * int ref > > The first adds an indirection for every access to foo and breaks > matching. The second adds an indirection for every mutable value in the > type. In both cases the extra indirections increase the memory footprint > and runtime. > > > So why not allow mutable in variant types and some equivalence with > records? For example: > > type <name> = <Constructor> of [mutable] <type>[:label] [| ...]
Please see [1], Alain Frisch has been working recently on implementing in-line records for constructor arguments. It's more implementation/design implications than people might think. [1] http://caml.inria.fr/mantis/view.php?id=5528 -- Wojciech -- Caml-list mailing list. Subscription management and archives: https://sympa-roc.inria.fr/wws/info/caml-list Beginner's list: http://groups.yahoo.com/group/ocaml_beginners Bug reports: http://caml.inria.fr/bin/caml-bugs