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 mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by yquem.inria.fr (Postfix) with ESMTP id 81DE2BC57 for ; Wed, 12 May 2010 14:06:27 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AiMCABM26kvRVaE2mGdsb2JhbACeHwgVAQEBAQEICQwHESKpSYIAhTsuiE4BAQMFgmOCKgQ X-IronPort-AV: E=Sophos;i="4.53,215,1272837600"; d="scan'208";a="50541267" Received: from mail-fx0-f54.google.com ([209.85.161.54]) by mail3-smtp-sop.national.inria.fr with ESMTP; 12 May 2010 14:06:26 +0200 Received: by fxm1 with SMTP id 1so766012fxm.27 for ; Wed, 12 May 2010 05:06:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:received:in-reply-to :references:date:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=zDNgvMTXvMRvoB3qk6J5yVqyvB8vKVfrVWVOTRmgUJQ=; b=xXVai/gDWjavagy64nK8kN99rcF7jclwcCWDo15bOR6dLSfeQPp+Rq6T+cx2lDhs7M UPn3sgj+LYQyzpbb4L2lrwI21grZabsUijn9HHtQo4bUTQHCLB1Wk+tz7F40TRmNdCg1 Jlnki3RDCKuYn6OZLKP4i1PtT1c/C7cq2A4YM= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; b=GxtWYjbXObsYJ/BS/CYMrNQ6+hqM59JPAD/+/HYVJZdqh17eTK/2Hml1EMeBhciHqk pMTbNjvwqIfssLR5ayBKRKLfAYN7LqZbiGhnIIv0yamxdihfz1p/nNi/0D6KU/yv94TD 14Vkx4016NIubVPBiT6zgxJswbHGZXdO+XmW0= MIME-Version: 1.0 Received: by 10.239.190.71 with SMTP id w7mr537209hbh.36.1273665986615; Wed, 12 May 2010 05:06:26 -0700 (PDT) Received: by 10.239.150.81 with HTTP; Wed, 12 May 2010 05:06:26 -0700 (PDT) In-Reply-To: References: Date: Wed, 12 May 2010 14:06:26 +0200 Message-ID: Subject: Re: [Caml-list] Ques from a beginner: how to access a type defined in one .ml file in another .ml file From: Philippe Wang To: Tarun Sethi Cc: caml-list@inria.fr Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Spam: no; 0.00; ocaml:01 mli:01 ocaml:01 compiler:01 compiler:01 printf:01 beginners':01 foo:01 avoided:01 syntax:01 unbound:01 unbound:01 wrote:01 caml-list:01 parentheses:01 On Wed, May 12, 2010 at 12:30 PM, Tarun Sethi wrot= e: > Hi, > > I m very new to ocaml and I am not sure if this the right forum to ask a > beginner level question. I have tried reading tutorials and the manual bu= t > no help. Please help me on the problem below, > > In a.ml a record type t is defined and is also defined transparently > in a.mli, i.e. in d interface so that the type definition is available > to all other files. > > a.ml also has a function, func, which returns a list of t. > > Now in another file, b.ml =A0i m calling func, now obviously ocaml > compiler wud nt be able to infer d type of objects stored in d list, > for compiler its just a list. so in b.ml, i hav something like dis, > > let tlist =3D A.func in > let vart =3D List.hd tlist in > printf "%s\n" vart.name =A0 =A0 (*name is a field in record t*) > > Now here i get a compiler error sayin "Unbound record field label > name" which makes sense as compiler can't infer d type of vart. > > my first question: how do I explicitly provide d type of vart as t > here? > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0i tried doing "let var= t:A.t =3D =A0" but got the > same error. > > I also tried creating another function to fetch the first element of d > list and mentioning return type as A.t, but then i got the "Unbound > value A.t". I did this: > > let firstt =3D function > =A0 =A0 =A0[] -> 0 > =A0 =A0| x :: _ -> A.t x ;; > > The problem is compiler is unable to recognize A.t (a type) in b.ml > but is able to recognize function A.func. If I remove A.t from the > b.ml, i don'get any compiler errors. > > Please help, its urgent work. > Thanks in advance! > > ~Tarun I guess this is not the right place to ask such a question... There is a beginners' list. However, this should answer your question : write instead : variable_name.Module_name.field_name If variable_name has been defined in yet another module, you may write YetAnotherModule.variable_name.Module_name.field_name If you want to avoid module name prefixes, you may want to use "open" : open Module_name;; let foo =3D variable_namefield_name ;; However (from my personal point of view) "open" should be avoided because it often makes maintenance very tough. About type constraints, the syntax is rather this : (variable : type_name) with parentheses most of the time. --=20 Philippe Wang mail@philippewang.info