F# 对表达的类型感到困惑
我有一个函数,我认为它应该返回一个类型T。然而,它是返回T->T的报告,在我看来它本身就是s函数。代码如下:F# 对表达的类型感到困惑,f#,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 | {
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
设置内容为T->T
摧毁是T->T
我对destroy进行了测试,返回的值是T
但是,雷管的类型是cell:T->\u arg1:T->T.\u arg1从何而来?这里,单词function并不是你想要的。相反,它本质上创建了一个匿名函数
大体上
function | ...
相当于
fun y -> match y with | ...
arg1对应于我添加的y
在您的情况下,将功能更改为:
你应该做你想做的
match cell with