groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Henrik Martin <hen...@netgate.net>
Subject Re: What am I missing with this Elvis operator expression?
Date Mon, 11 Apr 2016 22:43:43 GMT
And of course I meant to say "... and my println statement prints 10", 
not 910. :-)

-H

On 4/12/16 12:41 AM, Henrik Martin wrote:
> Greetings. I ran into this little surprise with the Elvis operator 
> today. I guess it must boil down to my misunderstanding of how the 
> operator work. I have a method that tries to calculate a value based 
> on an input. If the input value is set to -1, I'd like to change it to 
> a positive value (which is calculated, but in the test I just hard 
> coded it to 10). In the process of doing so, I ran into a surprise. 
> Here's a Spock test case that illustrates it:
>
> import spock.lang.Specification
>
> class ElvisTest extends Specification {
>   def "test"() {
>     setup:
>     def sleepFor = -1
>     sleepFor = sleepFor > 0 ?: 10
>     println sleepFor
>     sleepFor = 972
>     sleepFor = sleepFor > 0 ?: 10
>     println sleepFor
>   }
> }
> When sleepFor is initialized to -1, the operator works as I expected 
> it and my println statement prints 910. If I initialize sleepFor to 
> some other value like the one in the test, all of a sudden the result 
> of the Elvis operator is true instead of an integer. I naïvely thought 
> that ?: would yield the numeric value of the sleepFor variable in the 
> left hand expression instead of the boolean result of it (as in 
> sleepFor > 0). What am I missing? In this case, I could always use the 
> Java style of:
>
> sleepFor = sleepFor > 0 ? sleepFor : 10
>
> but the whole point of the Elvis operator is brevity, and this 
> behavior was a small surprise to me. Thanks,
>
> -H
>


Mime
View raw message