Generics f中的嵌套泛型约束#

Generics f中的嵌套泛型约束#,generics,f#,Generics,F#,我试图在泛型类(Xceed.Wpf.Toolkit.NumericUpDown)上定义一个扩展方法,但将泛型限制为Nullable,但是我无法理解语法。我想要的本质是 type NumericUpDown<Nullable<_>> with member x.getVal() = x.Value.GetValueOrDefault() member x.setVal v = x.Value <- Nullable v 键入带有 成员x.getVal()=x

我试图在泛型类(
Xceed.Wpf.Toolkit.NumericUpDown
)上定义一个扩展方法,但将泛型限制为
Nullable
,但是我无法理解语法。我想要的本质是

type NumericUpDown<Nullable<_>> with
  member x.getVal() = x.Value.GetValueOrDefault()
  member x.setVal v = x.Value <- Nullable v
键入带有
成员x.getVal()=x.Value.GetValuerDefault()

成员x.setVal v=x.Value我不知道您要扩展的类,但从F#3.1开始,您应该能够编写并使用以下内容:

open System.Runtime.CompilerServices

[<Extension>]
type NumericUpDownExtensions () =
  [<Extension>]
  static member getVal(x: NumericUpDown<Nullable<'a>>) = x.Value.GetValueOrDefault()
  [<Extension>]
  static member setVal(x: NumericUpDown<Nullable<'a>>, v) = x.Value <- Nullable v
open System.Runtime.CompilerServices
[]
类型NumericUpDownExtensions()=
[]

静态成员getVal(x:NumericUpDown>,v)=x.Value Hi-我最近看到您需要一个扩展方法-但遗憾的是,我不知道您试图扩展到足以显示代码的类。我能给出的最好的方法是运行时检查(
将x与|:?System.Nullable匹配)
),这在F风格的扩展中是不可能的。您不能扩展泛型类型的特定大小写。@Daniel仅供参考,请参见下文。右。可以使用.NET样式的扩展(通过
ExtensionAttribute
)。