8.12 Utilities

8.12.1 Routines iif

include std/utils.e
namespace utils
public function iif(atom test, object ifTrue, object ifFalse)

Used to embed an 'if' test inside an expression. iif stands for inline if or immediate if.

  1. test : an atom, the result of a boolean expression
  2. ifTrue : an object, returned if test is non-zero
  3. ifFalse : an object, returned if test is zero

An object. Either ifTrue or ifFalse is returned depending on the value of test.

Warning Note:
You must take care when using this function because just like all other Euphoria routines, this does not do any lazy evaluation. All parameter expressions are evaluated before the function is called, thus, it cannot be used when one of the parameters could fail to evaluate correctly. For example, this is an improper use of the iif method

first = iif(sequence(var), var[1], var)

The reason for this is that both var[1] and var will be evaluated. Therefore if var happens to be an atom, the var[1] statement will fail.
In situations like this, it is better to use the long method.

if sequence(var) then
     first = var[1]
     first = var
  end if
Example 1:
msg = sprintf("%s: %s", {
    iif(ErrType = 'E', "Fatal error", "Warning"),