caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Ques from a beginner: how to access a type defined in one .ml file in  another .ml file
@ 2010-05-12 10:30 Tarun Sethi
  2010-05-12 12:06 ` [Caml-list] " Philippe Wang
  0 siblings, 1 reply; 2+ messages in thread
From: Tarun Sethi @ 2010-05-12 10:30 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 1476 bytes --]

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 but
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  i 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 = A.func in
let vart = List.hd tlist in
printf "%s\n" vart.name     (*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?
                         i tried doing "let vart:A.t =  " 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 = function
     [] -> 0
   | 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

[-- Attachment #2: Type: text/html, Size: 2153 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [Caml-list] Ques from a beginner: how to access a type defined in  one .ml file in another .ml file
  2010-05-12 10:30 Ques from a beginner: how to access a type defined in one .ml file in another .ml file Tarun Sethi
@ 2010-05-12 12:06 ` Philippe Wang
  0 siblings, 0 replies; 2+ messages in thread
From: Philippe Wang @ 2010-05-12 12:06 UTC (permalink / raw)
  To: Tarun Sethi; +Cc: caml-list

On Wed, May 12, 2010 at 12:30 PM, Tarun Sethi <tarunsethi01@gmail.com> wrote:
> 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 but
> 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  i 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 = A.func in
> let vart = List.hd tlist in
> printf "%s\n" vart.name     (*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?
>                          i tried doing "let vart:A.t =  " 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 = function
>      [] -> 0
>    | 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 = 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.

-- 
Philippe Wang
   mail@philippewang.info


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2010-05-12 12:06 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-05-12 10:30 Ques from a beginner: how to access a type defined in one .ml file in another .ml file Tarun Sethi
2010-05-12 12:06 ` [Caml-list] " Philippe Wang

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).