Page 1 of 1

Learn how {expressions} work and useful Helper Functions

Posted: 13 Aug 2014, 09:50
by rednoah
If you're using format expression like {director} or {vc} you may have noticed that if a variable is not defined, it will cause the entire {expression} to fail and unwind.

The Unwind-on-Undefined Behaviour:

A common issue when writing expressions like this [{vc}, {ac}] is that it might evaluate to [, ] if the required bindings are undefined. A better way would be to write {"$vc, $ac"} (as String) or {[vc, ac]} (as List) in which case this single expression will only work if all required bindings are defined.

This expression will either evaluate successfully as a single unit, or fail as a single unit:

Code: Select all

{"A ${director} Movie"}
So if the binding is undefined, it would be evaluated like this and fail with an Exception:

Code: Select all

{"A ${throw new Exception()} Movie"}
Alternatively, if you access properties via the {self} binding you will not get this unwind-on-undefined behavior.

Code: Select all

{"A ${self.director} Movie"}
Which would evaluate like this if undefined:

Code: Select all

{"A ${null} Movie"} // A null Movie
This is useful if you want to check properties with an if-then-else statement:

Code: Select all

{"A ${self.director ?: 'No Name'} Movie"}

Helper Functions:
FileBot adds a few simple functions that can make your code much more easy to read if you need to collect the values of multiple bindings/expressions that may or may not fail and unwind.

Find and return the value of the first expression that evaluates successfully without returning null or throwing an Exception. If none of the expressions evaluates successfully null will be returned.

e.g. Take collection, or if collection is undefined take the first genre, or if no genres are defined use a fixed String value:

Code: Select all

{any{collection}{genres[0]}{'No collection or Genre'}}
Find and return all values of all the expressions that evaluate successfully and ignore all expressions that fail or unwind.

e.g. Take the values of multiple bindings and simply ignore those that are undefined:

Code: Select all

{allOf{match(/Extended.Edition/)}{vc}{ac}{source}{group} join '.'}
@see ExpressionFormatFunctions