Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Type inference problem
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: skaller <skaller@u...>
Subject: Re: [Caml-list] Type inference problem
On Tue, 2005-11-01 at 20:58 +1300, Jonathan Roewen wrote:
> Hi,
> 
> I can't figure out what's wrong with my code =(
> 
> It's on a paste site, so will only last about 24 hours or so.
> http://rafb.net/paste/results/Uux57B97.html
> 
> jonathan@moonbeam:~/dst/stdlib$ ocamlc VFS.ml
> File "VFS.ml", line 106, characters 3-6:
> This expression has type int but is here used with type unit
> 
> It -has- to return int ;-) But I have no idea where the type
> constraint is coming from that wants it to return unit. Change it to
> return unit, and where it's used complains it doesn't return type int
> (so that constraint is correct).

You could replace this line:

    let rec write data fd buf off len =

with this one:

    let rec write data fd buf off len : int =

to find out. But I can tell you anyhow the bug is here

   let rec read data fd buf off len =
      if len + fd.offset > fd.size then read data fd buf off (fd.size - fd.offset);

in the second line:

	if true then 1;

will give the same error for the same reason: the above line is just
shorthand for:

	if true then 1 else ();

and the type of 1 and () disagree. In your case, the type of the 'then'
branch is infered to be 'unit' .. and then you go and return
an 'int' at the end of the function -- there is no 'conflict'
until that point. Declaring the return type will catch the
error where it really is, the second line of the read function.

You can fix this by:

      if len + fd.offset > fd.size then 
	ignore(read data fd buf off (fd.size - fd.offset));

or by

      if len + fd.offset > fd.size then 
	let _ =  read data fd buf off (fd.size - fd.offset) in ();



-- 
John Skaller <skaller at users dot sf dot net>
Felix, successor to C++: http://felix.sf.net