[JEP-21] Ternary Conditional Expression#182
Merged
springcomp merged 1 commit intomainfrom May 23, 2025
Merged
Conversation
Contributor
Author
|
Build is broken… 😢 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Ternary Conditional Expression
Abstract
This JEP introduces a new
expressionto support an it-then-else ternary operation.Motivation
Most languages do have a ternary operator that is akin to an if-then-else syntax as a single statement.
For instance, many languages do have the following syntax:
While JMESPath does not have a ternary operator, the closest approximation using an expression like
( x || y ) && zunfortunately suffers from a limitation that makes it not really a true ternary operator. Specifically, if bothxandyare false, the expression still falls back to evaluatingz.More complicated, "object-wrapping" expressions are possible such as
(x && [y] || [z])[0]. Unfortunately, these appear to be more like a trick and are not user-friendly, especially for newcomers to the language.For those reasons, we propose introducing a true ternary operator to the language.
Grammar Changes
The following updates to the grammar support a ternary operation:
A
ternary-expression(short for ternary conditional expression) is an expression taking the following form:condition ? true-expression : false-expressionWhere
condition,true-expressionandfalse-expressionare JMESPath expressions.A ternary conditional expression will evaluate to either its "true" expression or its "false" expression.
If the evaluation of the condition is not false, then the "true" expression is evaluated and used as the return value.
Otherwise, the "false" epression is evaluated and used as the return value.
The following values are considered to be false values in JMESPath:
falseJSON valuenullJSON value""JSON string{}JSON objects[]JSON arrayExpressions within a ternary conditional expression are evaluated using a right-associative reading.
This means that a chain of ternary expression can be expressed without parentheses:
Precedence
The ternary condition expression is a new infix operator that has a higher precedence than the
|pipe expression and lower precedence than the||and&&logical expressions. Thus the precedence of various expressions goes like this, from lowest to highest:|pipe expression… ? … : …ternary conditional expression||OR logical expression&&AND logical expressionCompliance Tests
A new
ternary.jsontest file will be added to the suite of compliance tests.[ { "given": { "true": true, "false": false, "foo": "foo", "bar": "bar", "baz": "baz", "qux": "qux", "quux": "quux" }, "cases": [ { "expression": "true ? foo : bar", "result": "foo" }, { "expression": "false ? foo : bar", "result": "bar" }, { "expression": "`null` ? foo : bar", "result": "bar" }, { "expression": "`[]` ? foo : bar", "result": "bar" }, { "expression": "`{}` ? foo : bar", "result": "bar" }, { "expression": "'' ? foo : bar", "result": "bar" }, { "expression": "false ? foo | bar | @ : baz", "result": "baz" }, { "expression": "foo ? bar ? baz : qux : quux", "result": "baz" }, { "expression": "true || false ? foo : bar", "result": "foo" }, { "expression": "true && false ? foo : bar", "result": "bar" } ] } ]Alternatives
Although achieving the same result is possible within the current specification, the "array-wrapping" trick referred to in the motivation section is not user-friendly.
Other alternatives considered introducing a more general "switch-case-when-else" syntax but we feel that a ternary operator is so ubiquitous in most languages that it is worth introducing in the language. This does not preclude revisiting a more general syntax in the future.
Finally, we considered adding this feature as a function. Although this can be done today in most implementations that provide an extension point, we think a ternary operator is the most user-friendly least surprising way to include this feature.
Implementation Survey
Implementation is quite straighforward: