Basic Examples (2)
Evaluate a symbol with a value:
A symbol without value gives $Failed:
This expression only evaluates in its arguments, the head f has no effect:
FailIfUnevaluated detects this and considers this a trivial evaluation:
It does not simply test if the head remains the same. This is an example of a non-trivial evaluation that FailIfUnevaluated allows:
Scope (4)
If the head evaluates on its own but afterwards no further evaluation takes place, FailIfUnevaluated will give $Failed:
This fails because f is taken to be no more than a simple alias for g:
This will not fail:
However, if the whole expression transforms into another inert expression, this is still counted as a non-trivial evaluation:
Use a different default:
The default does not evaluate if it's not needed:
FailIfUnevaluated respects attributes of the head while testing for evaluation semantics:
This expression evaluates:
This expression doesn't evaluate because of the HoldFirst attribute, which causes FailIfUnevaluated to fail:
The action of the Listable and Flat attributes is considered non-trivial, even if the original head does not evaluate afterwards:
When the Orderless attribute sorts the arguments, this is counted as a trivial evaluation:
Applications (3)
Due to the symbolic nature of the language, many Wolfram Language functions stay unevaluated when given unsupported arguments:
This can be easily detected by FailIfUnevaluated:
Note that FailIfUnevaluated does not test for a specific return value. In the following examples, Or returns the same result, but the second example represents a partial evaluation which is considered non-trivial:
Symbolic math functions usually return unevaluated if they can't find a simpler result:
Detect this with FailIfUnevaluated:
Possible Issues (2)
Constant values and inert wrappers like List don't evaluate and thus return a failure:
However, some Wolfram expressions evaluate even when it's not immediately visible:
The FullForm demonstrates the difference:
Associations evaluate when constructed, which is considered to be a non-trivial step. Because of this, they behave differently from a list of rules:
The difference can be seen when wrapping an Association with Hold: