caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Goswin von Brederlow <goswin-v-b@web.de>
To: Mykola Stryebkov <nick@mykola.org>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Same name fields
Date: Sat, 21 Nov 2009 16:51:28 +0100	[thread overview]
Message-ID: <876393zxj3.fsf@frosties.localdomain> (raw)
In-Reply-To: <A9B74EBD-2FDD-4705-BE61-90C75E813529@mykola.org> (Mykola Stryebkov's message of "Sat, 21 Nov 2009 17:32:58 +0200")

Mykola Stryebkov <nick@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


  reply	other threads:[~2009-11-21 15:51 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-11-21 15:32 Mykola Stryebkov
2009-11-21 15:51 ` Goswin von Brederlow [this message]
2009-11-21 15:55 ` [Caml-list] " David Allsopp

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=876393zxj3.fsf@frosties.localdomain \
    --to=goswin-v-b@web.de \
    --cc=caml-list@inria.fr \
    --cc=nick@mykola.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).