List.sum uses static item restrictions. Static member constraints are not considered in extension methods, so this is not an option.
A wrapper of all complex type is an option, but it overflows, if it is just a specific call, you have many ways to calculate the sum with a few keystrokes, you can use fold , as shown in another answer. Alternatively, you can use List.reduce (+) if you are sure that there will always be at least one element in the list.
This may be fixed in a future version of F #, but the problem is that the static restrictions of members do not work with fields if they do not have a getter. However, in F # lib they can "emulate" those elements for existing types, they usually do this with primitive types , otherwise it would not work with int , float , since they also do not have this member.
I'm not sure that the fact that Complex defined in System.Numerics was the reason not to implement it that way, or maybe they just forgot it. In either case, you can open the problem or send a transfer request to fix it.
Finally, another option, if you still want to use it in a generic way, is to override the sum function. For example, the sum function (here the source ) from the last F # + will work fine (it had the same problem, but it was very easy to fix, in fact it was a mistake) with almost all numeric types, including Complex and most third-party numeric types since it is a backup mechanism that relies on some conversions when the type does not have a get_Zero member.
Gustavo Oct 29 '15 at 1:02 2015-10-29 01:02
source share