F# if not condition
What is the best way in F# to write an if not condition?
Right now I'm writing it like this:
if condition <> true then do
开发者_Python百科
Is there any other shorter way to write it? Like using the ! operator?
if you consider that not is also a function, then you can pipe your condition into it to avoid parenthesis like so:
if not <| condition param1 param2 then ...
reason being is if your condition function takes arguments, you don't need to do
not (condition param1 param2)
it's probably a little cleaner to do it the first way since it seems f# is in favor of pipes instead of parenthesis for operator precedence.
In Ocaml, you can use the "not" keyword:
if not condition then ...
Hopefully works too with F#.
There is the not function, but it only works with boolean variables.
So you can say:
if (not condition) then do
But that would not work with other types as in C-style languages.
Do not use !
by accident, as it is still an operator in F#, it is a dereference on a mutable reference cell.
See the full operator documentation.
I don't know F# but in C, using the ! operator I'd write:
if(!condition) { ... }
which is the same as
if(condition == false) { ... }
精彩评论