Mykola Stryebkov <n...@mykola.org> writes:

> Hi,
>
> I'm trying to declare to record types with fields having the same name
> but different types.
> Something like this:
>
> ========================================================================
>
> type ta = { a : int; b : string }
> type tb = { a : float; b : string list }
>
> let f v = Printf.printf "%d\n" v.a
>
> ========================================================================
>
> Compiler says I have an error in the line with Printf:
> This expression has type float but is here used with type int
>
> If I declare type ta second - everything works fine.
> Is it a bug? If not, what is an appropriate workaround here?
>
> Thanks in advance.

No, that is a feature. Just like

let x = 1 in
let x = 2 in
let x = 3 in
  Printf.printf "x = %d\n" x


As a workaround you can always put ta and tb into modules. Or you
create wrapper functions (with different names) to create and access
the records before overshadowing tb with ta like this:

type ta = { a : int; b : string }
let ta_make a b = { a = a; b = b; }
let ta_get_a ta = ta.a
let ta_get_b ta = ta.b

type tb = { a : float; b : string list }
let tb_make a b = { a = a; b = b; }
let tb_get_a tb = tb.a
let tb_get_b tb = tb.b

let f v = Printf.printf "%d\n" (ta_get_a v)

The drawback of the wrappers is that you can not match ta with { a =
a; b = b } -> .... So (sub)modules is really the best option if you
want the record fields to keep the same names.

MfG
        Goswin

_______________________________________________
Caml-list mailing list. Subscription management:
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
Archives: http://caml.inria.fr
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs

Reply via email to