2

我正在使用 F# 和 Xunit。(我对两者都比较陌生)

我发现当我使用 Xunit 的 Assert.Equal() 时,我需要指定"<string>"要比较的类型何时是字符串。

例如这个运行和编译:

[<Fact>]
let Test_XunitStringAssertion() =
    let s1 = "Stuff"
    Assert.Equal<string>("Stuff",s1)

我的问题是,为什么我不能删除"<string>"并只是断言"Assert.Equal("Stuff",s1)"呢?

在我看来编译器知道这两个参数的类型,那么为什么要大惊小怪呢?

以下是编译时返回的错误Assert.Equal("Stuff",s1)

error FS0041: A unique overload for method 'Equal' could not be determined based on type information prior to this program point. The available overloads are shown below (or in the Error List window). A type annotation may be needed.
error FS0041: Possible overload: 'Assert.Equal<'T>(expected: 'T, actual: 'T) : unit'.
error FS0041: Possible overload: 'Assert.Equal<'T>(expected: seq<'T>, actual: seq<'T>) : unit'.
error FS0041: Possible overload: 'Assert.Equal<'T>(expected: 'T, actual: 'T, comparer: System.Collections.Generic.IEqualityComparer<'T>) : unit'.
error FS0041: Possible overload: 'Assert.Equal(expected: float, actual: float, precision: int) : unit'.
error FS0041: Possible overload: 'Assert.Equal(expected: decimal, actual: decimal, precision: int) : unit'.
error FS0041: Possible overload: 'Assert.Equal<'T>(expected: seq<'T>, actual: seq<'T>, comparer: System.Collections.Generic.IEqualityComparer<'T>) : unit'.
4

2 回答 2

4

这是因为第一个和第二个重载string都可以匹配(记住:) 。string :> seq<char>

于 2012-02-25T19:48:10.087 回答
4

<string>正如我所期望的那样,您删除的示例对我来说没有错误(尽管string :> seq<char>正如@Ramon Snir 指出的那样,重载解析算法通过识别所提供的string类型“更接近” stringthan来解决歧义seq<char>)。

[<Fact>]
let Test_XunitStringAssertion() =
    let s1 = "Stuff"
    Assert.Equal("Stuff",s1)

我猜您提供的示例与导致您出现问题的真实代码不完全相同。也许s1在您的真实代码中实际上不是 a string(或者至少编译器不知道它是)。

于 2012-02-26T07:30:37.793 回答