Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a shorthand for Boolean `match` expressions?

Is there a shorthand for the match expression with isVertical here?

let bulmaContentParentTile isVertical nodes =
    let cssClasses =
        let cssDefault = [ "tile"; "is-parent" ]
        match isVertical with
        | true -> cssDefault @ [ "is-vertical" ]
        | _ -> cssDefault

    div [ attr.classes cssClasses ] nodes

I assume that an expression like match isVertical with is so common that there is a shorthand like the one we have for function, no?

like image 604
rasx Avatar asked Dec 11 '21 04:12

rasx


2 Answers

Yes, it's just an if-expression:

    let cssClasses =
        let cssDefault = [ "tile"; "is-parent" ]
        if isVertical then
            cssDefault @ [ "is-vertical" ]
        else cssDefault

Quoting from the 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.

like image 159
Brian Berns Avatar answered Oct 16 '22 09:10

Brian Berns


It's a bit off-topic but you can build your list by using Sequence Expressions which is IMHO more readable for this use case.

let cssClasses = [
    "tile"
    "is-parent"
    if isVertical then
        "is-vertical"
]
like image 29
Daniel Avatar answered Oct 16 '22 08:10

Daniel