One-line if-else statement with short-circuit evaluation

In Unix shell scripts, we can do the following

❯ [[ -n $HOME ]] && echo "true statement" || echo "else statement"
true statement
❯ [[ -n $NOT_A_ENV ]] && echo "true statement" || echo "else statement"
else statement

However, it does not work in Julia

julia> true && println("true statement") || println("else statement")
true statement
ERROR: TypeError: non-boolean (Nothing) used in boolean context
Stacktrace:
 [1] top-level scope
   @ REPL[15]:1

Is there a similar feature in Julia (in one line, of course)?

PS: I put short-circuit evaluation in the title as I suppose it were possible, but solution with other approaches is welcome as well :slight_smile:

The ternary operator is a one-line abbreviation for an if-else block.

julia> true ? println("true statement") : println("else statement")
true statement

julia> false ? println("true statement") : println("else statement")
else statement
3 Likes