From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by yquem.inria.fr (Postfix) with ESMTP id C2D43BBAF for ; Sat, 21 Nov 2009 16:51:32 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AkEBAPeaB0vZSMDdimdsb2JhbACBTZo7AQEBCgkMBxEFtwCEPAQ X-IronPort-AV: E=Sophos;i="4.47,264,1257116400"; d="scan'208";a="40545795" Received: from fmmailgate01.web.de ([217.72.192.221]) by mail1-smtp-roc.national.inria.fr with ESMTP; 21 Nov 2009 16:51:32 +0100 Received: from smtp06.web.de (fmsmtp06.dlan.cinetic.de [172.20.5.172]) by fmmailgate01.web.de (Postfix) with ESMTP id D4C5313C40465; Sat, 21 Nov 2009 16:51:31 +0100 (CET) Received: from [95.208.117.111] (helo=frosties.localdomain) by smtp06.web.de with asmtp (TLSv1:AES256-SHA:256) (WEB.DE 4.110 #314) id 1NBsFH-0000y4-00; Sat, 21 Nov 2009 16:51:31 +0100 Received: from mrvn by frosties.localdomain with local (Exim 4.69) (envelope-from ) id 1NBsFE-0002Hp-Da; Sat, 21 Nov 2009 16:51:28 +0100 From: Goswin von Brederlow To: Mykola Stryebkov Cc: caml-list@inria.fr Subject: Re: [Caml-list] Same name fields References: Date: Sat, 21 Nov 2009 16:51:28 +0100 In-Reply-To: (Mykola Stryebkov's message of "Sat, 21 Nov 2009 17:32:58 +0200") Message-ID: <876393zxj3.fsf@frosties.localdomain> User-Agent: Gnus/5.110006 (No Gnus v0.6) XEmacs/21.4.22 (linux) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Sender: goswin-v-b@web.de X-Sender: goswin-v-b@web.de X-Provags-ID: V01U2FsdGVkX1+kBA0vut1RB8fS+UqewHGZuhZ0k8fReSI5OZUU wppbECKQwWbYdmIqIJCqJs+W5x2kpDSm0lfSKY0eRJNuyz4Ns6 sdLBQL/hM= X-Spam: no; 0.00; printf:01 printf:01 compiler:01 bug:01 wrappers:01 mfg:98 caml-list:01 functions:01 int:01 int:01 writes:01 expression:02 modules:02 modules:02 match:02 Mykola Stryebkov 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