Consider implementing a "true" ternary operator #179
Replies: 3 comments 5 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. AbstractMotivationMost 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.
// TODO discuss precedence Compliance TestsAlternatives// TODO: this page discussioncomment-11824335 |
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