if I want to define a extension method for float array like standard deviation, would it be better to use module extension on Array module or extension on type float[]?
like :
module Array =
let std (arr: float[]) = ...
or
type float ``[]`` with
member this.std = ...
If I do type extension as the latter, would the std be only calculated once or every time it is used?
And, what is the right format for the latter, apprently type float ``[]`` with does not comile... thanks.
In this case, you can't define a type extension so the issue is moot - you must use an extension of the Array module. The reason that you can't define a type extension is that in F#, type extensions must exactly mirror type definitions, so you can define a type extension on the generic 'a list type, for instance, but not on the constructed type string list. Similarly, you could define an extension method on the (simulated) generic array type
'a ``[]``
but not on the constructed array type
float ``[]``
This behavior is different than C#, where it is possible to write extension methods on constructed generic types.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With