문제

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