F# 编译器生成的错误消息有时令人困惑。例如:

open Deedle
let inds = [1;   2;  6;  8; 11; 12]
let vals = [10; 20; 30; 40; 50; 60]
let siv = Series(inds, vals)
let fbminmax b (s: Series<float, float>) =
    if b then (Seq.min s.Values) else (Seq.max s.Values)
let sgi =
    siv
    |> Series.groupInto (fun i _ -> i % 2 = 0) fbminmax
printfn "%A" <| sgi
// error FS0001: Type mismatch. Expecting a
//    'bool -> Series<int,int> -> 'a'
// but given a
//    'bool -> Series<int,int> -> float'
// The type 'float' does not match the type 'int'

我知道有一个错误(代码工作正常,Series<int,int> 替换 Series<float,float> 定义中的 fbminmax )。我明白一个
'bool -> Series<int,int> -> 'a'
预料之中。但我不明白为什么编译器说它被赋予了
'bool -> Series<int,int> -> float'
当它被赋予 fbminmax 时,这是一个
'bool -> Series<float,float> -> float'
此外,如果编译器确实得到了一个
'bool -> Series<int,int> -> float'
正如它声称的那样,当 float 扮演 'a 的角色时,应该没问题。

有人可以提供一些关于正在发生的事情的见解吗?

最佳答案

编译器错误消息确实有些神秘。解决方案:使其通用:-)

let fbminmax b (s: Series<'T, 'T>) =
    if b then (Seq.min s.Values) else (Seq.max s.Values)

最后的问题是您指定了 fbminmax b (s: Series<float, float>) 但将一系列 int 输入其中。

关于compiler-errors - 我无法弄清楚这个 F# 错误消息,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43126144/

10-12 00:13
查看更多