F#类型推断错过了给定的信息

时间:2013-06-27 20:50:57

标签: f# type-inference type-annotation

如果我宣布这个F#功能:

let extractColumn col (grid : List<Map<string, string>>) =
    List.map (fun row -> row.[col]) grid

编译器抱怨:

  

错误FS0752:运算符'expr。[idx]'已根据此程序点之前的信息用于不确定类型的对象。考虑添加更多类型约束

为lambda的row参数添加类型注释会修复它:

let extractColumn col (grid : List<Map<string, string>>) =
    List.map (fun (row : Map<string, string>) -> row.[col]) grid

为什么它不能从row函数的extractColumn参数中获取grid的类型?

1 个答案:

答案 0 :(得分:8)

F#的类型推断从左到右,从上到下。

grid部分中没有List.map (fun row -> row.[col])的类型。

使用管道运算符|>

let extractColumn col (grid : Map<string, string> list) =
    grid |> List.map (fun row -> row.[col])

使您的示例按预期工作。