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

Jochen Theodorou commented on GROOVY-8200:
------------------------------------------

I think you are let astray a bit by the fact, that we are talking about 
booleans. But those binary operators are not specific to booleans at all. I can 
do for example {code:Java}
class X {
  def foo
  def or(X x) {new X(foo:foo+" or "+x.foo)}
  String toString(){foo}
}

def a = new X(foo:"to be")
def b = new X(foo:"not to be ...")
println a|b
{code}and I would get "to be or not to be".

Now, what would you expect as result in this if a is null, or b is null or if 
both are null? And why is it different for boolean?

Also please do not forget, that Groovy truth is applied automatically only in 
very certain well known cases. null||x works, because the compiler enforces a 
boolean case for the operands. Then does x||null and even null||null. It does 
not matter what type x is here, because of this enforcement. But this also 
means not having a method behind this operator. This is completely extended by 
the compiler in exactly one way and only this way. The same goes for example 
for "if (x)...". Here we do know again we require a boolean and the compiler 
will cause a boolean cast to ensure this.

> Shorthand |= results in NPE
> ---------------------------
>
>                 Key: GROOVY-8200
>                 URL: https://issues.apache.org/jira/browse/GROOVY-8200
>             Project: Groovy
>          Issue Type: Bug
>    Affects Versions: 2.3.11
>         Environment: Groovy Version: 2.3.11 JVM: 1.7.0_80 Vendor: Oracle 
> Corporation OS: Linux
> java version "1.7.0_80"
> Java(TM) SE Runtime Environment (build 1.7.0_80-b15)
> Java HotSpot(TM) 64-Bit Server VM (build 24.80-b11, mixed mode)
>            Reporter: Daniel Hammer
>            Assignee: Jochen Theodorou
>         Attachments: shorthand_and.groovy, shorthand_or_symptom.groovy, 
> shorthand_xor_symptom.groovy
>
>
> I've stumbled across some curious behaviour for the shorthand assignment 
> operator in Groovy v2.3.11 (symptom also present in Groovy v2.4.9).
> The following [boolean or logic|^shorthand_or_symptom.groovy], unexpectedly 
> throws {{NullPointerException}}.
> {code}boolean x = null
> // short hand unrolled:  x = null || x
> x |= null // -> unexpected NullPointerException
> assert !x{code}
> The same approach for [boolean and logic|^shorthand_and.groovy], performs as 
> expected.
> {code}boolean x = null
> // short hand unrolled:  x = null && x
> x &= null
> assert !x{code}
> It's possible to work around the issue with explicit cast.
> {code}x |= null as boolean{code}



--
This message was sent by Atlassian JIRA
(v6.3.15#6346)

Reply via email to