F# &引用;无效约束:用于约束的类型是密封的;无效
我希望使用F#的类型推断来简单地通过调用的方法/函数推断代码的类型 这可以通过使用表单的扩展方法很好地实现F# &引用;无效约束:用于约束的类型是密封的;无效,f#,F#,我希望使用F#的类型推断来简单地通过调用的方法/函数推断代码的类型 这可以通过使用表单的扩展方法很好地实现 [<Extension>] type AttributeExtensions = [<Extension>] static member ``BroadcastOffset``<'a, 'b when 'a :> ``BroadcastOffset``<'b>> (this: 'a) = this.``
[<Extension>]
type AttributeExtensions =
[<Extension>]
static member ``BroadcastOffset``<'a, 'b when 'a :> ``BroadcastOffset``<'b>> (this: 'a) =
this.``
类型推断将启动,并推断x,必须确实是BroadcastOffset类型我使用您的代码得到编译错误,然后将我认为您需要的内容分解为一个函数,然后在扩展中使用该函数。这使我能够调用
.Value()
,结果发现它属于'a option类型,我想你是在追求它
open System.Runtime.CompilerServices
let v (x : 'a option) = x.Value
[<Extension>]
type AttributeExtensions =
[<Extension>]
static member Value(this) = v(this)
let myFunc o = o.Value()
open System.Runtime.CompilerServices
设v(x:'a选项)=x.值
[]
类型AttributeExtensions=
[]
静态成员值(this)=v(this)
设myFunc o=o.Value()
我必须承认我不确定我是否理解你的问题。您想定义一个扩展方法,这样当您使用它时,它所在的类型被推断为一个选项?ie让myFunc o=o.Value()/'a选项->'a
?这是扩展方法中的“.Value()”吗?-在这种情况下,扩展方法不是编译器…或者“x.Value”是选项、、上定义的方法,在这种情况下,它不会编译,因为f#不知道x的类型。我问的问题是,在哪里尝试了解更多关于您试图实现的目标,这对我来说仍然不清楚。我能够无错误地运行下面的代码。希望你在找什么。真奇怪!。。是的,我的代码不会编译…这就是重点…这是非常奇怪的“静态成员值(this)=(this:u选项)”也能工作,我真的不明白为什么会发生这种情况,但谢谢
x.Value
[<Extension>]
type AttributeExtensions =
[<Extension>]
static member Value<'a,'b when 'a :> Option<'b>> (this: 'a) =
this.Value
x.Value()
open System.Runtime.CompilerServices
let v (x : 'a option) = x.Value
[<Extension>]
type AttributeExtensions =
[<Extension>]
static member Value(this) = v(this)
let myFunc o = o.Value()