Consider implementing a "true" ternary operator #179
Replies: 3 comments 6 replies
-
Although I think adding true-ternary syntax (or even more general if/else/switch/case/etc.) is worthwhile, it is actually possible to achieve the functionality right now with an array-wrapping trick.
|
Beta Was this translation helpful? Give feedback.
-
Ternary Operator ExpressionThis is a work-in-progress draft for an upcoming proposal to introduce a ternary operator (or expression) to the language. Please, use this discussion page to discuss the merits and other finer points about this draft. AbstractThis JEP introduces a new MotivationMost languages do have a ternary operator that is akin to an if-then-else syntax as a single statement.
While JMESPath does not have a ternary operator, the closest approximation using an expression like More complicated, "object-wrapping" expressions are possible such as For those reasons, we propose introducing a true ternary operator expression to the language. Grammar ChangesThe following updates to the grammar support a ternary operation: expression =/ ternary-expression
ternary-expression = expression "?" expression ":" expression A
Where The following values are considered to be false values in JMESPath:
Expressions within a ternary operator expression are evaluated using a right-associative reading.
Compliance TestsA new [
{
"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" }
]
}
] AlternativesAlthough 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. |
Beta Was this translation helpful? Give feedback.
-
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" }
]
}
] |
Beta Was this translation helpful? Give feedback.
-
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:
Unfortunately JMESPath does not have a ternary operator.
The closest approximation suffers from a limitation that makes it not really a true ternary operator.
Please, consider introducing ternary operator to the language.
Beta Was this translation helpful? Give feedback.
All reactions