tags:

views:

74

answers:

1

Hello,

In simple words, I want to implement a function like that:

let myfunction (scalar : float) (quantities : ''T list) = 
    List.fold (fun acc quantity -> acc + quantity * scalar)
        LanguagePrimitives.GenericZero<_>
            quantities : 'T

Note that I am multiplying a value of generic type 'T and a float value (quantity * scalar) which should return a value of type 'T. For example, multiplying a Point by a scalar result in a Point.

I cannot make F# understand that the multiplication quantity * scalar should result in a value of type 'T. I tried LanguagePrimitives.MultiplyDynamic, but it seems that this function is reserved to be used by the F# compiler.

What should I do?

+3  A: 

Is there any reason to specialize to the case where scalar is a float? I think your best bet is to generalize even further:

let inline myfunction scalar quantities =  
    List.fold (fun acc quantity -> acc + quantity * scalar)
        LanguagePrimitives.GenericZero<_>
        quantities 
kvb
Nice, the inline resolved my problems. Amazing how easy you can write generalized code in F#! I am getting passionate for this language everyday!
Allan