I want to add my own indexer to generic array type:

type 'T``[]`` with
    member this.Item(x: string) = 1  // test

However, this gives a compile error "expression was expected to have type int but here has type string (FS0001)."

let a = [|1|]
let b = a.["aa"]      // error: FS0001
let c = a.Item("aa")  // this works.

I found this question which was asked 3 years ago and the answer suggested it's an FSharp bug. Want to confirm if it is still the case, or whether the specification has been updated since then?

有帮助吗?

解决方案

I got this question answered by Don Syme here:

The specification needs to be clarified that indexers for arrays may not be extended.

I'd say the suggestion to allow them is best tracked via http://fslang.uservoice.com. That said, it is not simple to do this, because array indexers are "built in" to the compiler and have no F# or IL metadata representation.

So it's not supported in Fsharp.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top