Generics 如何防止推断类型为obj(或错误)
下面的代码让我有些头疼Generics 如何防止推断类型为obj(或错误),generics,f#,Generics,F#,下面的代码让我有些头疼 type Config() = class end type ProgressA<'a>(v: 'a) = class end type DoneA<'a>(v:'a) = class end type Foo () = class end type ProgressX = ProgressA<Foo> type DoneX = DoneA<Foo> let somethingElse = 1 type Foo wi
type Config() = class end
type ProgressA<'a>(v: 'a) = class end
type DoneA<'a>(v:'a) = class end
type Foo () = class end
type ProgressX = ProgressA<Foo>
type DoneX = DoneA<Foo>
let somethingElse = 1
type Foo with
static member inline Validate (_:Config) (p:ProgressX) : Option<ProgressX> = Some p
let inline validatex c p =
(^T : (static member Validate: ^V -> ^P -> Option<ProgressA< ^T>>) c, p)
let p1: ProgressX = Unchecked.defaultof<_>
let v1: Config = Unchecked.defaultof<_>
let c = validatex v1 p1
到
告诉我无法为
p1
找到方法Validate
,噢,我的天哪!我找到了解决方案…一句话:元组 问题似乎是,只有在成员函数的参数是单元组参数的情况下,类型推断器才能在这种情况下正确运行
type Foo with
//see the double parens
static member inline Validate ((_:Config, p:ProgressX)): Option<ProgressX> = Some p
//And then you need a lot of parens here as well
let inline validatex c p =
(^T : (static member Validate: (^V * ProgressA< ^T>) -> Option<ProgressA< ^T>>) ((c, p)))
使用
//看到双帕伦夫妇了吗
静态成员内联验证((:Config,p:ProgressX)):Option=Some p
//然后你也需要很多父母
让我们内联验证excp=
(^T:(静态成员验证:(^V*ProgressA<^T>)->Option>)((c,p)))
let inline validatex c p =
(^T : (static member Validate: ^V -> ProgressA< ^T> -> Option<ProgressA< ^T>>) c, p)
let c = validatex v1 p1
type Foo with
//see the double parens
static member inline Validate ((_:Config, p:ProgressX)): Option<ProgressX> = Some p
//And then you need a lot of parens here as well
let inline validatex c p =
(^T : (static member Validate: (^V * ProgressA< ^T>) -> Option<ProgressA< ^T>>) ((c, p)))