[ 
https://issues.apache.org/jira/browse/GROOVY-12442?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18129421#comment-18129421
 ] 

ASF GitHub Bot commented on GROOVY-12442:
-----------------------------------------

codecov-commenter commented on PR #2996:
URL: https://github.com/apache/groovy/pull/2996#issuecomment-6307942785

   ## 
[Codecov](https://app.codecov.io/gh/apache/groovy/pull/2996?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)
 Report
   :x: Patch coverage is `88.88889%` with `4 lines` in your changes missing 
coverage. Please review.
   :white_check_mark: Project coverage is 72.4376%. Comparing base 
([`8778bce`](https://app.codecov.io/gh/apache/groovy/commit/8778bce1a0da05b5bffe27f02819262c849f9d56?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache))
 to head 
([`fcdb4e8`](https://app.codecov.io/gh/apache/groovy/commit/fcdb4e8d9ec4641ddd977d300fe79cb2f959480b?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)).
   
   | [Files with missing 
lines](https://app.codecov.io/gh/apache/groovy/pull/2996?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)
 | Patch % | Lines |
   |---|---|---|
   | 
[...roovy/transform/stc/StaticTypeCheckingVisitor.java](https://app.codecov.io/gh/apache/groovy/pull/2996?src=pr&el=tree&filepath=src%2Fmain%2Fjava%2Forg%2Fcodehaus%2Fgroovy%2Ftransform%2Fstc%2FStaticTypeCheckingVisitor.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-c3JjL21haW4vamF2YS9vcmcvY29kZWhhdXMvZ3Jvb3Z5L3RyYW5zZm9ybS9zdGMvU3RhdGljVHlwZUNoZWNraW5nVmlzaXRvci5qYXZh)
 | 25.0000% | [1 Missing and 2 partials :warning: 
](https://app.codecov.io/gh/apache/groovy/pull/2996?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)
 |
   | 
[...rc/main/java/org/codehaus/groovy/syntax/Types.java](https://app.codecov.io/gh/apache/groovy/pull/2996?src=pr&el=tree&filepath=src%2Fmain%2Fjava%2Forg%2Fcodehaus%2Fgroovy%2Fsyntax%2FTypes.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-c3JjL21haW4vamF2YS9vcmcvY29kZWhhdXMvZ3Jvb3Z5L3N5bnRheC9UeXBlcy5qYXZh)
 | 66.6667% | [0 Missing and 1 partial :warning: 
](https://app.codecov.io/gh/apache/groovy/pull/2996?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)
 |
   
   <details><summary>Additional details and impacted files</summary>
   
   
   
   [![Impacted file tree 
graph](https://app.codecov.io/gh/apache/groovy/pull/2996/graphs/tree.svg?width=650&height=150&src=pr&token=1r45138NfQ&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)](https://app.codecov.io/gh/apache/groovy/pull/2996?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)
   
   ```diff
   @@                Coverage Diff                 @@
   ##               master      #2996        +/-   ##
   ==================================================
   + Coverage     72.4290%   72.4376%   +0.0086%     
   - Complexity      40622      40636        +14     
   ==================================================
     Files            1643       1643                
     Lines          142686     142705        +19     
     Branches        26799      26803         +4     
   ==================================================
   + Hits           103346     103372        +26     
   + Misses          29830      29825         -5     
   + Partials         9510       9508         -2     
   ```
   
   | [Files with missing 
lines](https://app.codecov.io/gh/apache/groovy/pull/2996?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)
 | Coverage Δ | |
   |---|---|---|
   | 
[...va/org/apache/groovy/parser/antlr4/AstBuilder.java](https://app.codecov.io/gh/apache/groovy/pull/2996?src=pr&el=tree&filepath=src%2Fmain%2Fjava%2Forg%2Fapache%2Fgroovy%2Fparser%2Fantlr4%2FAstBuilder.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-c3JjL21haW4vamF2YS9vcmcvYXBhY2hlL2dyb292eS9wYXJzZXIvYW50bHI0L0FzdEJ1aWxkZXIuamF2YQ==)
 | `87.1124% <100.0000%> (ø)` | |
   | 
[...ehaus/groovy/ast/expr/ElvisOperatorExpression.java](https://app.codecov.io/gh/apache/groovy/pull/2996?src=pr&el=tree&filepath=src%2Fmain%2Fjava%2Forg%2Fcodehaus%2Fgroovy%2Fast%2Fexpr%2FElvisOperatorExpression.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-c3JjL21haW4vamF2YS9vcmcvY29kZWhhdXMvZ3Jvb3Z5L2FzdC9leHByL0VsdmlzT3BlcmF0b3JFeHByZXNzaW9uLmphdmE=)
 | `95.8333% <100.0000%> (+2.9762%)` | :arrow_up: |
   | 
[...us/groovy/classgen/asm/BinaryExpressionHelper.java](https://app.codecov.io/gh/apache/groovy/pull/2996?src=pr&el=tree&filepath=src%2Fmain%2Fjava%2Forg%2Fcodehaus%2Fgroovy%2Fclassgen%2Fasm%2FBinaryExpressionHelper.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-c3JjL21haW4vamF2YS9vcmcvY29kZWhhdXMvZ3Jvb3Z5L2NsYXNzZ2VuL2FzbS9CaW5hcnlFeHByZXNzaW9uSGVscGVyLmphdmE=)
 | `89.4385% <100.0000%> (+0.2493%)` | :arrow_up: |
   | 
[...rc/main/java/org/codehaus/groovy/syntax/Types.java](https://app.codecov.io/gh/apache/groovy/pull/2996?src=pr&el=tree&filepath=src%2Fmain%2Fjava%2Forg%2Fcodehaus%2Fgroovy%2Fsyntax%2FTypes.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-c3JjL21haW4vamF2YS9vcmcvY29kZWhhdXMvZ3Jvb3Z5L3N5bnRheC9UeXBlcy5qYXZh)
 | `74.1117% <66.6667%> (+0.0659%)` | :arrow_up: |
   | 
[...roovy/transform/stc/StaticTypeCheckingVisitor.java](https://app.codecov.io/gh/apache/groovy/pull/2996?src=pr&el=tree&filepath=src%2Fmain%2Fjava%2Forg%2Fcodehaus%2Fgroovy%2Ftransform%2Fstc%2FStaticTypeCheckingVisitor.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-c3JjL21haW4vamF2YS9vcmcvY29kZWhhdXMvZ3Jvb3Z5L3RyYW5zZm9ybS9zdGMvU3RhdGljVHlwZUNoZWNraW5nVmlzaXRvci5qYXZh)
 | `87.1992% <25.0000%> (ø)` | |
   
   ... and [11 files with indirect coverage 
changes](https://app.codecov.io/gh/apache/groovy/pull/2996/indirect-changes?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)
   </details>
   <details><summary> :rocket: New features to boost your workflow: </summary>
   
   - :snowflake: [Test 
Analytics](https://docs.codecov.com/docs/test-analytics): Detect flaky tests, 
report on failures, and find test suite problems.
   - :package: [JS Bundle 
Analysis](https://docs.codecov.com/docs/javascript-bundle-analysis): Save 
yourself from yourself by tracking and limiting bundle sizes in JS merges.
   </details>




> Introduce a null-coalescing operator (??) that provides a default value only 
> when the left-hand operand is null, independently of Groovy Truth.
> -----------------------------------------------------------------------------------------------------------------------------------------------
>
>                 Key: GROOVY-12442
>                 URL: https://issues.apache.org/jira/browse/GROOVY-12442
>             Project: Groovy
>          Issue Type: New Feature
>          Components: syntax
>            Reporter: Jefferson H
>            Priority: Minor
>
> h2. Motivation
> Groovy currently provides the Elvis operator ({{{}?:{}}}) for default values. 
> However, its behavior relies on Groovy Truth, which treats values such as 
> {{{}0{}}}, {{{}false{}}}, empty strings, and empty collections as false.
> This behavior is useful in many situations, but it can also lead to 
> unexpected results when those values are intentional and valid.
> *For example:*
>  
> {code:java}
> def retries = 0
> def result = retries ?: 3
> assert result == 3 {code}
>  
> In this case, {{0}} is a valid configuration value representing no retries, 
> but the Elvis operator replaces it with {{{}3{}}}.
> Currently, developers must use an explicit null check:
>  
> {code:java}
> def result = retries != null ? retries : 3 {code}
>  
> This introduces unnecessary verbosity for a common use case.
> h2. Proposed Syntax
> Introduce the {{??}} operator:
>  
> {code:java}
> def result = value ?? defaultValue {code}
>  
> The right-hand expression should be evaluated only when the left-hand 
> expression evaluates to {{{}null{}}}.
> h2. Expected Behavior
>  
> {code:java}
> assert (null ?? 10) == 10
> assert (0 ?? 10) == 0
> assert (false ?? true) == false
> assert ('' ?? 'default') == ''
> assert ([] ?? [1, 2]) == []
> assert ([:] ?? [enabled: true]) == [:] {code}
> Short-circuit evaluation
>  
> The left operand should be evaluated exactly once, and the right operand 
> should only be evaluated when necessary.
> {code:java}
> def calls = 0
> def fallback = {
>     calls++
>     return 10
> }
> assert (0 ?? fallback()) == 0
> assert calls == 0
> assert (null ?? fallback()) == 10
> assert calls == 1
>   {code}
> h2. Comparison with Existing Elvis Operator
> ||Value||{{value ?: default}}||{{value ?? default}}||
> |{{null}}|default|default|
> |{{0}}|default|{{0}}|
> |{{false}}|default|{{false}}|
> |{{''}}|default|{{''}}|
> |{{[]}}|default|{{[]}}|
> |Non-empty value|original|original|
> h2. Benefits
>  * Provides explicit null-only fallback semantics.
>  * Preserves valid falsy values without additional conditional expressions.
>  * Improves readability and reduces boilerplate.
>  * Complements the existing Elvis operator rather than replacing it.
>  * Aligns Groovy with familiar null-coalescing syntax found in languages such 
> as C#, JavaScript, and PHP.
>  * Helps avoid subtle configuration bugs involving counters, flags, and empty 
> values.
> h2. Compatibility Considerations
> This proposal does not require changing the existing behavior of Groovy 
> Truth, {{{}?:{}}}, or {{{}?={}}}.
> The new operator should work with dynamically typed and statically compiled 
> Groovy code.
> Operator precedence, associativity, interaction with other conditional 
> operators, and parser compatibility would need to be defined as part of the 
> implementation.
> h2. Optional Future Enhancement
> A null-coalescing assignment operator ({{{}??={}}}) could be considered 
> separately:
> {code:java}
> def retries = 0
> retries ??= 3
> assert retries == 0 {code}
> The initial proposal focuses only on the {{??}} operator.
> h2. Acceptance Criteria
>  * {{??}} returns the left-hand value whenever it is not {{{}null{}}}.
>  * {{??}} returns the right-hand value when the left-hand value is 
> {{{}null{}}}.
>  * The left-hand expression is evaluated exactly once.
>  * The right-hand expression is evaluated lazily.
>  * Falsy non-null values are preserved.
>  * Existing Elvis operator behavior remains unchanged.
>  * The operator is supported by the Groovy parser and static compilation.
>  * Tests and language documentation are added.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to