Work with all numeric types (int, float, long)

this simple function:

let sum a b = a + b

will only work for int types

how to make it work for float and long too?

+3
source share
2 answers

Use inline :

let inline sum a b = a + b

UPDATE:

If you are interested in writing your own polymorphic number functions, you should use inline and LanguagePrimitives .

Here is a polymorphic cosine function from a stream Converting a Haskell polymorphic cosine function to F # :

let inline cosine n (x: ^a) = 
    let one: ^a = LanguagePrimitives.GenericOne
    Seq.initInfinite(fun i -> LanguagePrimitives.DivideByInt (- x*x) ((2*i+1)*(2*i+2)))
    |> Seq.scan (*) one
    |> Seq.take n
    |> Seq.sum
+8
source

, int - ; int, . float long, inline, Pad, :

let sumFloat (a:float) b = a + b

let sumLong (a:int64) b = a + b

- , " , ", .

+3

All Articles