简体   繁体   English

lambda 表达式中逗号的 F# 语法

[英]F# syntax of comma within lambda expression

I'm new to F#.我是 F# 的新手。 Can someone explain what the following lambda expression is saying?有人可以解释以下 lambda 表达式的含义吗? Specifically, what the comma after the Some c.Id is doing and what all the parenthesis are about?具体来说, Some c.Id的逗号是做什么的,所有括号都是关于什么的?

(fun ((b, p), c) -> (b && p.SelectedInnerRow = Some c.Id, c))

I'm a bit confused coming from C#.来自 C# 的我有点困惑。

Thanks.谢谢。

The parentheses are for deconstruction of tuples.括号用于解构元组。 The lambda expression takes a tuple of (a tuple and some value) and some other value; lambda 表达式接受一个元组(一个元组和一些值)和一些其他值; and it returns yet another tuple.它返回另一个元组。

Now for the types involved - as the argument named b is operand to the boolean operator && , it must be of type bool .现在对于所涉及的类型 - 因为名为b的参数是布尔运算符&&操作数,所以它必须是bool类型。 The argument p has to have a property named SelectedInnerRow , which in turn ought to produce an option.参数p必须有一个名为SelectedInnerRow的属性,该属性又应该产生一个选项。 This option is equality-compared to another option value of argument c 's property Id , so the whole expression will return a tuple of a bool and the unchanged argument c .)此选项与参数c的属性Id另一个选项值进行相等比较,因此整个表达式将返回一个 bool 元组和未更改的参数c 。)

(* Dummy types to make it work *)
type Foo = { SelectedInnerRow : int option }
type Bar = { Id : int }

(fun ((b, p), c) -> (b && p.SelectedInnerRow = Some c.Id, c))
(* val it : _arg1:(bool * Foo) * c:Bar -> bool * Bar *)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM