Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F# module extension vs type entension

Tags:

f#

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.

like image 264
matlabdbuser Avatar asked May 20 '11 14:05

matlabdbuser


1 Answers

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.

like image 177
kvb Avatar answered Nov 15 '22 09:11

kvb