Search code examples
typescompiler-errorsreason

reason-ml is this a type inference issue?


I'm currently trying Reason and facing an error I don't understand

This is my code:

let mult = (x:float, y:float):float => x * y;

When I compile it with BuckleScript I get the following error:

  We've found a bug for you!
  D:\1\bbl\orga\src\demo.re 1:40

  1 Ôöé let mult = (a:float, b:float):float => a * b;

  This has type:
    float
  But somewhere wanted:
    int

You can convert a float to a int with int_of_float.If this is a literal, you want a number without a trailing dot (e.g. 20).

I don't see why the compiler would need to turn a float into an int here


Solution

  • My recommendation is to remove the types and let inference handle it, it is really good.

    But if you want to keep your specific types, then you need to change your operator. What you have is multiplying two integers. If you wanted to multiply two floats, you change it to:

    let mult = (x:float, y:float):float => x *. y;
    

    Notice the operator went from * to *. to indicate floating point math.

    See more documentation here: https://caml.inria.fr/pub/docs/manual-ocaml/libref/Pervasives.html#1_Floatingpointarithmetic