[
https://issues.apache.org/jira/browse/GROOVY-8857?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]
Eric Holley updated GROOVY-8857:
--------------------------------
Description:
The Elvis operator is great in that is simplifies your code and makes it easier
to read. The problem is that when the referenced object is a String or Number,
and you want to use a default value when the object is null, unintended results
can occur due to the null String and zero being false in Groovy truth. I find
myself having to frequently use more syntactically complex code due this the
issue.
My proposal is to introduce some new operators to Groovy to address this:
The first would be variation of the Elvis operator '??:' that would work just
like '?:' except that the test for truth would always be 'is the referenced
object null'. So, for example:
{code:java}
def xyz = abc ??: 'default'{code}
would be compiled as:
{code:java}
def xyz = abc != null ? abc : 'default' {code}
The second would be a unary '?' operator that would look like:
{code:java}
if (?abc) {
// Do something if abc is not equal null.
}{code}
which would be compiled as:
{code:java}
if (abc != null) {
// Do something if abc is not equal null.
}{code}
The third would be the non-null Elvis assignment operator:
{code:java}
a ??= 'default'{code}
which would compile as:
{code:java}
a = a != null ? a : 'default'{code}
This class of operators could be referred to as the 'non-null' operators. I
think that they would be a good addition to the new 3.x operators.
was:
The Elvis operator is great in that is simplifies your code and makes it easier
to read. The problem is that when the referenced object is a String or Number,
and you want to use a default value when the object is null, unintended results
can occurs due to the null String and zero being false in Groovy truth. I find
myself have to frequently use more syntactically complex code due this the
issue.
My proposal is to introduce some new operators to Groovy to address this:
The first would be variation of the Elvis operator '??:' that would work just
like '?:' except that the test for truth would always be 'is the referenced
object null'. So, for example:
{code:java}
def xyz = abc ??: 'default'{code}
would be compiled as:
{code:java}
def xyz = abc != null ? abc : 'default' {code}
The second would be a unary '?' operator that would look like:
{code:java}
if (?abc) {
// Do something if abc is not equal null.
}{code}
which would be compiled as:
{code:java}
if (abc != null) {
// Do something if abc is not equal null.
}{code}
The third would be the non-null Elvis assignment operator:
{code:java}
a ??= 'default'{code}
which would compile as:
{code:java}
a = a != null ? a : 'default'{code}
This class of operators could be referred to as the 'non-null' operators. I
think that they would be a good addition to the new 3.x operators.
> Add various 'object != null' syntactic sugar operators.
> -------------------------------------------------------
>
> Key: GROOVY-8857
> URL: https://issues.apache.org/jira/browse/GROOVY-8857
> Project: Groovy
> Issue Type: Improvement
> Components: Compiler
> Affects Versions: 3.x
> Reporter: Eric Holley
> Priority: Minor
>
> The Elvis operator is great in that is simplifies your code and makes it
> easier to read. The problem is that when the referenced object is a String or
> Number, and you want to use a default value when the object is null,
> unintended results can occur due to the null String and zero being false in
> Groovy truth. I find myself having to frequently use more syntactically
> complex code due this the issue.
> My proposal is to introduce some new operators to Groovy to address this:
> The first would be variation of the Elvis operator '??:' that would work just
> like '?:' except that the test for truth would always be 'is the referenced
> object null'. So, for example:
> {code:java}
> def xyz = abc ??: 'default'{code}
> would be compiled as:
> {code:java}
> def xyz = abc != null ? abc : 'default' {code}
> The second would be a unary '?' operator that would look like:
> {code:java}
> if (?abc) {
> // Do something if abc is not equal null.
> }{code}
> which would be compiled as:
> {code:java}
> if (abc != null) {
> // Do something if abc is not equal null.
> }{code}
> The third would be the non-null Elvis assignment operator:
> {code:java}
> a ??= 'default'{code}
> which would compile as:
> {code:java}
> a = a != null ? a : 'default'{code}
> This class of operators could be referred to as the 'non-null' operators. I
> think that they would be a good addition to the new 3.x operators.
--
This message was sent by Atlassian Jira
(v8.3.4#803005)