Conditional expressions and Boolean operators #175
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
It looks like Shopify are considering/experimenting with conditional expressions in Liquid. This PR implements that same syntax and behaviour in Python Liquid.
Here we're talking about in line control structures that can be used in output statements, assignment expressions and as arguments to tags and filters. Liquid currently supports the following expressions in these locations.
true
,false
,nil
/null
)(1..5)
)foo.bar[0]
)empty
,blank
)Collectively, in Python Liquid, we call these primitive expressions. They are the small building blocks that make up all other expressions.
Shopify/Liquid #1922 (draft) adds support for compound expressions in output statements, assignment expressions and as arguments to tags and filters. These compound expressions combine primitives with Boolean operators (
and
andor
) and/or comparison operators (==
,>
,>=
, etc.).Boolean operator semantics
In this example, the variable
x
will be set to the value ata
ifa
is truthy, or the value atb
ifa
is falsy.This is logically equivalent to
if a then a else b
ora if a else b
using ternary condition syntax.And here, with the
and
operator, the variablex
will be set to the value atb
ifa
is truthy, or the value ata
ifa
is falsy, which is logically equivalent toif a then b else a
orb if a else a
using ternary condition syntax.With short-circuit evaluation, these Boolean operator semantics are sometimes called "last value", as the result is the value of the last sub expression to be evaluated.
Note
Outstanding issue Shopify/liquid #1034 might limit the usefulness of in line Boolean operators for some deployments.
This experiment
As we already support ternary condition syntax, we'll conceptually treat expressions including Boolean operators as a shorthand form of the more general ternary expression.
We'll define a primary expression to replace most uses of primitive expression that is effectively equivalent to the current
parse_logical_primitive()
function, just that the resulting expression evaluates to the last value rather than a Boolean.Existing comparison operators should work unchanged, because they always evaluate to a Boolean value.