0

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

glennsl
  • 28,186
  • 12
  • 57
  • 75
netlyonel
  • 155
  • 1
  • 2
  • 8

2 Answers2

6

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

Zoe
  • 27,060
  • 21
  • 118
  • 148
Neil Kistner
  • 11,623
  • 2
  • 18
  • 17
2

I don’t know reason but is not the * operator reserved to integer as in ocaml ? Try the *. operator instead

Aldrik
  • 136
  • 6