You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Eric Milles (Jira)" <ji...@apache.org> on 2022/05/02 18:44:00 UTC
[jira] [Commented] (GROOVY-10535) IF condition on empty Collection has different behavior than null Collection
[ https://issues.apache.org/jira/browse/GROOVY-10535?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17530896#comment-17530896 ]
Eric Milles commented on GROOVY-10535:
--------------------------------------
I am able to recreate with Java 8/11 and Groovy 3.0.10. Groovy 2.5.16 ran as expected for 200,000 and 500,000 iterations.
> IF condition on empty Collection has different behavior than null Collection
> ----------------------------------------------------------------------------
>
> Key: GROOVY-10535
> URL: https://issues.apache.org/jira/browse/GROOVY-10535
> Project: Groovy
> Issue Type: Bug
> Affects Versions: 3.0.10
> Environment: Groovy 3.0.10/OpenJDK 17.0.2 - Ubuntu 21.10
> Reporter: Rodolfo Yanke
> Priority: Major
>
> I believe this code should print "something" but doesn't work:
> {code:java}
> @CompileStatic
> class NotWorkingExample {
> static void main(String[] args) {
> Collection<String> values = null
> for (i in 0..<200_000) {
> printSomethingIfNotEmpty(values)
> }
> //never printed but it should
> values = ['A']
> printSomethingIfNotEmpty(values)
> }
> static printSomethingIfNotEmpty(Collection<String> values) {
> if(values) {
> println 'something'
> }
> }
> }
> {code}
> This one does print "something" because we pass an empty collection [] instead of null:
> {code:java}
> @CompileStatic
> class ItWorks {
> static void main(String[] args) {
> Collection<String> values = []
> for (i in 0..<200_000) {
> printSomethingIfNotEmpty(values)
> }
> //it works because [] was passed in the previous 200k calls
> values = ['A']
> printSomethingIfNotEmpty(values)
> }
> static printSomethingIfNotEmpty(Collection<String> values) {
> if(values) {
> println 'something'
> }
> }
> }{code}
> Some optimization is done differently when the condition is skipped too many times. Both classes should output "something" on the last method call.
> Thank you for the support.
--
This message was sent by Atlassian Jira
(v8.20.7#820007)