假設我已經實現了索引器類型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
,但它似乎不工作:
let a = A([| 1;2;3 |])
a.[1]
a.[1] <- 10
a.[1.0]
最後一行,我得到:
Script1.fsx(243,4): error FS0001: This expression was expected to have type
int
but here has type
float
是擴大索引可能在F#中?謝謝!
尼斯抓,托馬斯! – 2011-03-08 11:26:32
感謝Tomas!我發送了一封電子郵件給FSharp Bug Reports。 – 2011-03-08 12:04:34