Extending the Indexer for an existing class

回眸只為那壹抹淺笑 提交于 2019-12-06 10:53:12

This behaves differently when the type extension is defined in a separate assembly (or separate module) and when it is in the same module as the type definition.

  • When both are in the same module, F# compiles them into a single class and Item becomes a standard overloaded indexer - In this case, your code works as expected (and this is how you actually wrote it here).

  • When they are in separate modules, F# compiles the indexer as an extension member. In this case, I get the error message you described.

Adding new overloads using extension members (e.g. new method) is possible. As far I can see, the specificaton doesn't say that this shouldn't work for indexers, so I think it is a bug (can you report it to fsbugs at microsoft dot com?)

I just tried this in FSI and it seems to work. What compiler are you using? This is what I fed to FSI:

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] <- 10
printfn "%A" a.[1.2]

This prints '10'

The error description says it pretty clearly - expected int, given float, so the problem is 1.0, if you replace that with 1, it should work.

1 is int

1.0 is float

1.0f is float32, aka double in some languages

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!