如何为非泛型数组定义扩展方法?
How to define an extention method for a non-generic array?
我无法弄清楚在非泛型数组上定义扩展方法的正确语法是什么。 This question 处理通用数组,但尝试对字节数组使用类似的构造是行不通的。
我尝试了很多选择:
type Byte ``[]`` with
type byte ``[]`` with
type Byte array with
type byte array with
type array<Byte> with
type array<byte> with
type []<Byte> with
type []<byte> with
以及所有这些包含在双反引号或括号中的构造,但没有任何效果。我下载了语言规范,但它只有一个通用数组示例。
这可能是可选类型扩展的一个怪癖,can get pretty funky when generics are involved。我会改用这样的扩展方法:
open System.Runtime.CompilerServices
[<Extension>]
type ByteArrayExtensions =
[<Extension>]
static member inline Sum(xs: byte[]) = Array.sum xs
let f() =
let xs = [| byte(1); byte(2); byte(3) |]
xs.Sum()
我无法弄清楚在非泛型数组上定义扩展方法的正确语法是什么。 This question 处理通用数组,但尝试对字节数组使用类似的构造是行不通的。
我尝试了很多选择:
type Byte ``[]`` with
type byte ``[]`` with
type Byte array with
type byte array with
type array<Byte> with
type array<byte> with
type []<Byte> with
type []<byte> with
以及所有这些包含在双反引号或括号中的构造,但没有任何效果。我下载了语言规范,但它只有一个通用数组示例。
这可能是可选类型扩展的一个怪癖,can get pretty funky when generics are involved。我会改用这样的扩展方法:
open System.Runtime.CompilerServices
[<Extension>]
type ByteArrayExtensions =
[<Extension>]
static member inline Sum(xs: byte[]) = Array.sum xs
let f() =
let xs = [| byte(1); byte(2); byte(3) |]
xs.Sum()