假设我有一个带有索引器的类型A,例如类型A是一个库.现在我想扩展它的索引器,例如在这里我想在索引器中添加浮点数.
我制定了以下代码:
type A(a:int array) =
member this.Item
with get(x) = a.[x]
and set(x) value = a.[x] <- value
type A with
member m.Item with
get(x:float) = m.[x |> int]
and set(x:float) v = m.[x |> int] <- v
Run Code Online (Sandbox Code Playgroud)
但它似乎不起作用:
let a = A([| 1;2;3 |])
a.[1]
a.[1] <- 10
a.[1.0]
Run Code Online (Sandbox Code Playgroud)
对于最后一行,我得到:
Script1.fsx(243,4): error FS0001: This expression was expected to have type
int
but here has type
float
Run Code Online (Sandbox Code Playgroud)
是否可以在F#中扩展索引器?谢谢!
f# ×1