A simple F# ternary operator
While porting some C# to F# recently I got sick of rewriting ternary operators like this
var result = condition ? "True Branch" : "False Branch";
into a form like this
let result = if condition then "True Branch" else "False Branch"
or
let result = match condition with true -> "True Branch" | false -> "False Branch"
Thankfully, although F# lacks an inbuilt ternary operator it does allow you to define your own operators
/// Custom ternary operator
let (?) truth trueBranch falseBranch =
if truth then trueBranch else falseBranch
let test condition =
(?)
condition
"True branch"
"False branch"