F # vs type entension module extension

if I want to define an extension method for a float array, for example, standard deviation, would it be better to use a module extension on a module or an Array extension on a type float[] ? eg:

 module Array = let std (arr: float[]) = ... 

or

 type float ``[]`` with member this.std = ... 

If I use a type extension as the last, will std be evaluated once or every time it will be used?

And, what is the correct format for the latter, perhaps type float ``[]`` with does not compile ... thanks.

+6
source share
1 answer

In this case, you cannot define a type extension, so the question is debatable - you should use the Array module extension. The reason you cannot define a type extension is because in F # extension types must accurately reflect type definitions, so you can define a type extension for the generic type 'a list , for example, but not for the constructed type string list . Similarly, you can define an extension method for a type (simulated) general type

 'a ``[]`` 

but not by constructed array type

 float ``[]`` 

This behavior is different from C #, where you can write extension methods for the constructed generic types.

+5
source

Source: https://habr.com/ru/post/888643/


All Articles