# F# generic add function

**URL:** <https://forums.fsharp.org/t/f-generic-add-function/1246>\
**Category:** General\
**Created:** [March 31, 2020, 1:35am UTC](https://forums.fsharp.org/t/f-generic-add-function/1246 "2020-03-31T01:35:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![DamianReeves](https://yyz2.discourse-cdn.com/flex030/user_avatar/forums.fsharp.org/damianreeves/32/193_2.png) [@DamianReeves](https://forums.fsharp.org/u/DamianReeves)\
**Post date:** [March 31, 2020, 1:35am UTC](https://forums.fsharp.org/t/f-generic-add-function/1246/1 "2020-03-31T01:35:12Z")

</div>

I’m working on a project which requires us to have a “standard library” across various languages/platforms. Portions of this “standard library” map very close to the `elm/core` library for the Elm platform/language.

One function I need to define is add. In Elm they have a numeric “type class”, but F# doesn’t support this. How can I define a generic add function in my module that works for different numeric types? The code-snippet below shows the issue. For all functions below I was able to map the equivalent Elm functions to F# and F# infers the types properly. The add function however always warns, with this message:

> FS0064: This construct causes code to be less generic than indicated by the type annotations.

How can I fix this (Please note I will be applying this solution to define subtract, multiply, etc)

```auto
[<AutoOpen>]
    module Morphir.SDK.Basics

    let (<|) = Microsoft.FSharp.Core.Operators.(<|)
    let (|>) = Microsoft.FSharp.Core.Operators.(|>)

    let add (a:^a) (b:^b) :^c = (+) a b

    let inline abs (n:^a) =
        Microsoft.FSharp.Core.Operators.abs(n)

    let inline pow (x:^a) (n:int) =
        Microsoft.FSharp.Core.Operators.pown x n

```

---

<div class="post-metadata">

**Author:** ![DamianReeves](https://yyz2.discourse-cdn.com/flex030/user_avatar/forums.fsharp.org/damianreeves/32/193_2.png) [@DamianReeves](https://forums.fsharp.org/u/DamianReeves)\
**Post date:** [March 31, 2020, 3:10am UTC](https://forums.fsharp.org/t/f-generic-add-function/1246/2 "2020-03-31T03:10:52Z")

</div>

So this seems to work:

```auto
let inline add (value1 : ^T when ^T : (static member (+) : ^T * ^T -> ^T)) (value2: ^T) =
    value1 + value2

```

---

<div class="post-metadata">

**Author:** ![FoggyFinder](https://yyz2.discourse-cdn.com/flex030/user_avatar/forums.fsharp.org/foggyfinder/32/136_2.png) [@FoggyFinder](https://forums.fsharp.org/u/FoggyFinder)\
**Post date:** [March 31, 2020, 5:56am UTC](https://forums.fsharp.org/t/f-generic-add-function/1246/3 "2020-03-31T05:56:53Z")

</div>

I suppose you can use simple `inline` here as well:

```auto
let inline add a b = (+) a b

```
