Postfix conditional operator in F#

f#, postfix-operator

Solution

There is no built-in F# support for this and so I would recommend just using the normal prefix style conditionals (or pattern matching). There is also no standard operator/function that would be common in the community, so the idiomatic style would just use ordinary `if`.

I think that the best you can do is something like this:

/// Evaluates lazy value only if the specified boolean holds
let assuming b (v:Lazy<_>) = if b then v.Value

// Print foo only when x > 40
let foo() = printfn "hi"
let x = 42
lazy foo() |> assuming (x > 40)

Note that I had to add `lazy` to the expression (to make sure it is not actually evaluated when the condition does not holds). This works, but it is certainly uglier than writing `if x>40 then foo()` - but it is a fun thing to experiment with :-)

Problem

In Perl language one can write something like ``` someFunction() if $x == 0 ``` i.e. apply condition in postfix notation. I was sure there must be similar type of expression in F#, since it is so flexible in working with functions. But when I try to write ``` someFunction() if x = 0 ``` or ``` someFunction() << if x = 0 ``` I recieve an expected error message. Is there any way to implement more or less general postfix conditional operator in F#?

Original source