我希望能够使用静态方法从其他库扩展类型以启用通用算术。VectorN以Microsoft新推出的 SIMD 友好的固定大小类型为例。他们定义Zero,他们定义(+),他们定义(/),但我不能Array.average在他们身上使用,因为他们没有定义DivideByInt,我很乐意:
open System.Numerics
type Vector2f with
static member DivideByInt (v:Vector2f) (i:int) = v / Vector2f(single i, single i)
let bigArray : Vector2f[] = readABigFile()
printf "the average is %A" (Array.average bigArray)
但它不会让我编译,抱怨
error FS0001: The type 'Vector2f' does not support the operator 'DivideByInt'
为什么 F# 编译器中存在此限制?
(编辑:基本上之前问过同样的问题。)