[Opa] mixing float & int in calculations

Alok Menghrajani alok at fb.com
Thu Nov 17 10:44:45 EST 2011


Hi,

Am I doing it wrong if I'm often mixing float and int in my calculations
and I'm calling Int.of_float & Int.to_float all the time? I have reached a
point where I almost wish opa would let me multiply a float with an int;
or have I just been coding too much in php?

On a related note, here is something that surprised me. If I try to
compile the following piece of code:

1 start() = (
2   do inrange(0, 9, (i ->
3     a:int = Int.of_float(i * 1.5)
4     b:int = i * 2
5     Debug.warning("{a} {b}")))
6   <>Blah</>
7 )
8 server = Server.one_page_bundle("Simple", [], [], start)



I get an error:

Error
File "simple2.opa", line 4, characters 12-17, (4:12-4:17 | 83-88)
Function was found of type 'a, 'a -> 'a but application expects it to be of
type float, int -> 'b.
Types float and int are not compatible


I think it's incorrect for the compiler to complain about line 4, when the
mistake is on line 3. It seems the type system is not taking into account
that the iterator inside inrange is of type int, and that line 3 is
therefore incorrect. (I actually ran into a variation of this, but line 4
for much further down the road and it took me a while to figure out what
was going on...).



Alok



More information about the Opa mailing list