【发布时间】:2012-11-05 00:09:26
【问题描述】:
我有一个已经实现 .Item 方法的 .Net 库,例如
namespace Library2
type A() =
member m.Item with get(a: string) = printfn "get a string"
member m.Item with get(a: int) = printfn "simple slice"
在使用这个库的代码中,我想添加一个额外的同名方法(因此是optional extensions):
#r @"Library2.dll"
open Library2
type A with
member m.Item with get(a: bool) =
printfn "get a bool"
以下示例的最后一行无法编译:
let a = new A()
a.["good"]
a.[10]
a.[true]
F# doc 说:
扩展方法不能是虚拟或抽象方法。他们能 重载其他同名方法,但编译器给出 在不明确的调用情况下优先使用非扩展方法。
这意味着我不能用相同的类型签名扩展.ToString/.GetHashCode,但这里我使用不同的类型签名。为什么新方法不能扩展?
【问题讨论】:
-
我觉得奇怪的是 Intellisense 显示了所有三个重载。
-
是的。这让我很困惑......
标签: f# extension-methods