Version française
Home     About     Download     Resources     Contact us    
Browse thread
[newbie] Define and use records in sum types
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: David.Mentre@i...
Subject: [newbie] Define and use records in sum types
Hello dear camlists,

I have problems with basic record and sum types use.

I would like to define a sum type with records as constructor
parameters, records with the same field names. 

I have tried the follwing definition:

<ocaml>
# type asp = A of { name : string } | B of { name : string ; ext : int };;
                  -
Syntax error
</ocaml>

However, I can use intermediate names:

<ocaml>
# type a_rec = { name : string };;
type a_rec = { name : string; } 
# type b_rec = { name : string ; ext : int };;
type b_rec = { name : string; ext : int; } 
# type asp = A of a_rec | B of b_rec ;;
type asp = A of a_rec | B of b_rec
</ocaml>

It *seems* to work but, in fact, the a_rec definition is masked by b_rec
definition:

<ocaml>
# let a_b = B({name = "n" ; ext=3 }) ;;
val a_b : asp = B {name="n"; ext=3}
# let an_a = A({name="n"});;
               ----------
Some record field labels are undefined
</ocaml>

So my question is: is it possible to define a sum of records with the
same field names? 


The rationale behind this question: I find records more clear that
tuples in source code.


Best regards,
david
-- 
 David.Mentre@irisa.fr -- http://www.irisa.fr/prive/dmentre/
 Opinions expressed here are only mine.