布尔“匹配”表达式是否有 shorthand?
Is there a shorthand for Boolean `match` expressions?
这里有 match
表达式 isVertical
的 shorthand 吗?
let bulmaContentParentTile isVertical nodes =
let cssClasses =
let cssDefault = [ "tile"; "is-parent" ]
match isVertical with
| true -> cssDefault @ [ "is-vertical" ]
| _ -> cssDefault
div [ attr.classes cssClasses ] nodes
我假设像 match isVertical with
这样的表达式很常见,以至于有一个 shorthand 就像我们为 function
所用的那样,不是吗?
是的,它只是一个 if
-表达式:
let cssClasses =
let cssDefault = [ "tile"; "is-parent" ]
if isVertical then
cssDefault @ [ "is-vertical" ]
else cssDefault
引用自 F# docs:
Unlike in other languages, the if...then...else
construct is an expression, not a statement. That means that it produces a value, which is the value of the last expression in the branch that executes.
这有点离题,但您可以使用 Sequence Expressions 来构建您的列表,恕我直言,对于此用例,它更具可读性。
let cssClasses = [
"tile"
"is-parent"
if isVertical then
"is-vertical"
]
这里有 match
表达式 isVertical
的 shorthand 吗?
let bulmaContentParentTile isVertical nodes =
let cssClasses =
let cssDefault = [ "tile"; "is-parent" ]
match isVertical with
| true -> cssDefault @ [ "is-vertical" ]
| _ -> cssDefault
div [ attr.classes cssClasses ] nodes
我假设像 match isVertical with
这样的表达式很常见,以至于有一个 shorthand 就像我们为 function
所用的那样,不是吗?
是的,它只是一个 if
-表达式:
let cssClasses =
let cssDefault = [ "tile"; "is-parent" ]
if isVertical then
cssDefault @ [ "is-vertical" ]
else cssDefault
引用自 F# docs:
Unlike in other languages, the
if...then...else
construct is an expression, not a statement. That means that it produces a value, which is the value of the last expression in the branch that executes.
这有点离题,但您可以使用 Sequence Expressions 来构建您的列表,恕我直言,对于此用例,它更具可读性。
let cssClasses = [
"tile"
"is-parent"
if isVertical then
"is-vertical"
]