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#?
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:
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 writingif x>40 then foo()
- but it is a fun thing to experiment with :-)