Error when trying to define a generic function

Help please!!

If you hover over the addGen function (you might have to temporarily comment out line 25 so the file compiles) you can see that it has the signature addGen : Additive a => a -> a -> a so as desired it is generic here.

Now when you try to use it in line 25 the compiler needs to figure out which type a should be. You are using a literal with a decimal point which means that it will be a value of type Numeric n for some number n. The numeric type allows for different scales (number of digits after the decimal point). A very common case is Decimal which is a synonym for Numeric 10. In your example there is no further information to help the compiler figure out which n you want. If you add a type annotation and change line 25 as follows , the error goes away:

assert (addGen (6.0 : Numeric 10) 4.0 == 10.0)
1 Like

Could this also be solved by making the addGen type signature less generic? (ie. is there a type that handles all kinds of numbers?)

Also @Vivek_Srivastava please post example code as text and not screenshots so that those answering can copy/paste the code to test it, and those searching the forums have an easier time finding it.

You can put all your code between 3 backticks (```) so it gets formatted nicely, so for example entering the following:

```
addGen x y = x + y
assert (addGen 6.0 4.0 == 10.0)
```

Would give you:

addGen x y = x + y
assert (addGen 6.0 4.0 == 10.0)
1 Like

Sure you can make it less generic but the question was explicitly about defining a generic function.

If you want to make it less generic, you can go for

addGen : Numeric 10 -> Numeric 10 -> Numeric 10
addGen x y = x + y
1 Like