4

考虑以下使用 F# 4.0、.NET 4.6 的代码片段:

type X<'T> = Y of 'T

type XS = X<string>
type XI = X<int>

type X<'T when 'T :> string> with
    static member X = 2
    static member take (s: 'T) = s

[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module XS =
    let foo = 10
    let create s = XS.Y s
    let test = XI.take 2    // expected only string allowed, XI.take should not exist

我希望类型扩展type X<'T when 'T :> string> with得到尊重(在这种情况下,这意味着错误,因为string是密封的,或者限制'T为 be string),或者引发语法错误。

更重要的是,我也可以使用以下语法,这将是正常类型定义中的语法错误(没有with):

type X<'T> when 'T :> string with
    static member X = 2
    static member take (s: 'T) = s

我的猜测是,扩展上的约束被简单地忽略了。这是设计使然吗?或者它应该工作,如果是这样,如何?

当我尝试使用类型扩展并想知道我是否可以创建一组仅适用于特定具体类型或进一步受限的具体类型的特定方法时,我想到了这一切(这也可以通过继承来完成,我知道)。

4

1 回答 1

3

我认为编译器应该拒绝你的代码,但这里有一些观察:

  1. 请注意,如果您使用“外部”扩展(例如,通过将定义和扩展放在不同的模块中 - 请参阅类型扩展),行为会有所不同 - 在那里,编译器会将扩展标记为错误。
  2. 如果您想要拥有仅针对类型参数的某些具体值显示的实例扩展方法,那么您可以使用 C# 样式的扩展方法(这也在上面的链接中进行了说明)。但是,由于 C# 不支持静态扩展成员,因此无法将此技巧应用于您的方案。
于 2016-10-13T14:32:29.390 回答