Skip to content
Discussions/App Development/Error when trying to define a generic functionForum ↗

Error when trying to define a generic function

App Development4 posts395 views3 likesLast activity Apr 2020
VI
Vivek_SrivastavaOP
Apr 2020

Help please!!

CO
cocreature
Apr 2020

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)
AN
anthony
Apr 2020

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)
CO
cocreature
Apr 2020

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
← Back to Discussions