[Initial Feedback] New Conditional Expression
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Idoneità per principianti
- 25/100
- Tipo di issue
- Funzionalità
- Chiarezza
- Da chiarire
- Stato di attività
- Ferma
- Ambito
- compilers
Direzione di ricerca
Inizia esaminando le modifiche proposte alla grammatica, le regole di precedenza e gli esempi dei test di conformità in questo JEP. L’issue menziona l’aggiunta di un nuovo test di conformità ternary.json, ma non identifica i file di implementazione né un punto di ingresso; per completarlo sarebbero necessarie una proposta accettata e le corrispondenti modifiche al linguaggio e ai test di conformità.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Ternary Conditional Expression
This 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.
Abstract
This JEP introduces a new expression to 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:
<condition> ? <true-result> : <false-result>
While JMESPath does not have a ternary operator, the closest approximation using an expression like ( x || y ) && z unfortunately suffers from a limitation that makes it not really a true ternary operator. Specifically, if both x and y are false, the expression still falls back to evaluating z.
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:
expression =/ ternary-expression
ternary-expression = expression "?" expression ":" expression
A ternary-expression (short for ternary conditional expression) is an expression taking the following form:
condition ? true-expression : false-expression
Where condition, true-expression and false-expression are 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:
- The boolean
falseJSON value - The
nullJSON value - An empty
""JSON string - An empty
{}JSON objects - An empty
[]JSON array
Expressions 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:
a ? b :
c ? d :
e
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 expression
Compliance Tests
A new ternary.json test 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.
History
| Date | Description |
|---|---|
| 2025-05-16 | Renamed "Ternary operation expression" to "Ternary conditional expression" |
| 2025-05-16 | Added mention of operator precedence and clarified with new compliance tests. |
- Lingua principale
- Python
- Stelle
- 10
- Fork
- 3
- Metriche di merge delle PR
- Nessuna PR unita negli ultimi 30g
Preparare l'ambiente
Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di jmespath/jmespath.jep
-
Difficoltà 5/5 Più di una settimana Idoneità per principianti 30/100
jmespath/jmespath.jep#36 ·
-
Root node accessAperta
Difficoltà 5/5 Più di una settimana Idoneità per principianti 25/100
jmespath/jmespath.jep#34 · 1 commento ·
-
Difficoltà 5/5 Più di una settimana Idoneità per principianti 25/100
jmespath/jmespath.jep#33 ·
-
Allow string comparisonAperta
Difficoltà 5/5 Più di una settimana Idoneità per principianti 25/100
jmespath/jmespath.jep#32 ·
-
Difficoltà 5/5 Più di una settimana Idoneità per principianti 35/100
jmespath/jmespath.jep#31 · 3 commenti · 9 reazioni ·
Tutte le issue di jmespath/jmespath.jep
Issue simili
-
test_real_pandoc_failure_is_one_path_free_line fails on macOS CI with current Homebrew pandocApertabug TAG: unit tests
Difficoltà 2/5 1-3 ore Idoneità per principianti 65/100
learningequality/ricecooker#898 ·
-
task
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
freelawproject/litigant-portal#1285 ·
I maintainer di solito rispondono entro 2 giorni
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 65/100
jmespath/jmespath.py#401 ·
-
least_squares returns a nonfinite objective for an invalid initial residualForse già presa @sylvesterkaczmarek l’ha presa oggi. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
google-deepmind/mujoco#4023 ·
I maintainer di solito rispondono entro 11 giorni
-
Five Australian birds: Red-capped Parrot, Red-winged Fairywren, Gilbert’s Honeyeater, Western Spinebill, Western WattlebirdForse già presa Una pull request collegata a questa issue è aperta o già unita. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 66/100
arnegiacomo/fugleramme#427 ·
I maintainer di solito rispondono entro 1 giorno