对表达的类型感到困惑

时间:2014-08-13 03:35:10

标签: f#

我有一个函数,我认为应该返回一个类型T.但是,它报告的是返回(T - > T),这对我来说确实看起来像s函数。这是代码:

let setContent content cell = { cell with Content = content }

let destroy = setContent Crater

let detonateMine cell = 
    function 
    | { Content = Mine } -> 
        destroy cell
    | { Content = Crater } -> 
        let errMessage = sprintf "The cell has already been denotated. (%i, %i)" cell.Location.X cell.Location.Y
        invalidOp errMessage
    | _ -> 
        let errMessage = 
            sprintf "The cell cannot be denotated. It does not contain a mine. (%i, %i)" cell.Location.X 
                cell.Location.Y
        invalidOp errMessage

setContent是T - > Ť

销毁是T - > Ť

我有针对destroy的测试,返回的值为T.

然而,引爆的类型是cell:T -> _arg1:T -> T。 _arg1来自哪里?

1 个答案:

答案 0 :(得分:7)

在这里,function这个词并不能做你想要的。相反,它基本上创建了一个匿名函数。

一般

function | ...

相当于

fun y -> match y with | ...

_arg1对应{I}添加的y

在您将function更改为:

的情况下
match cell with

应该做你想做的事。

相关问题