如何重载静态方法并使用泛型类型约束

时间:2017-02-27 15:56:01

标签: f# type-constraints

给出以下类型及其重载方法

type ToOptionString = ToOptionString
with
    static member ($) (_: ToOptionString, l: string) : Option<string> = Some l
    static member ($) (_: ToOptionString, l: Option<string>) : Option<string> = l
    static member ($) (_: ToOptionString, l: Option<int>) : Option<string> = 
       l |>> sprintf "%A"

let inline toOptionString v = ToOptionString $ v

编译本身很好

但是如果我在另一个模块中使用函数toOptionString

let x = toOptionString (Some 1)

我收到以下错误

A unique overload for method 'op_Dollar' could not be determined based on 
type information prior to this program point. A type annotation may be 
needed. Candidates: 
static member ToOptionString.( $ ) : ToOptionString * l:Option<int> -> Option<string>, 
static member ToOptionString.( $ ) : ToOptionString * l:Option<string> -> Option<string>

val toOptionString : v:'a -> '_arg3 (requires member ( $ ))

所以在我看来,当重载时我不能使用泛型类型参数的类型。有人知道解决这个问题吗?

0 个答案:

没有答案