Slackbot
10/07/2022, 9:13 PMdaniel
11/09/2022, 9:07 PMifPresent
implementation is wrong. It should getOrNull()
, check if not null, then call the block with the value previously retrieved via getOrNull()
. The current implementation will break if the provider returns a value on first get and null on subsequent get or something along those line. Although pretty rare, ignoring this potential behaviour is leaking abstraction as you would be assuming how the provider is calculated which is wrong.Chris Lee
11/09/2022, 9:09 PMisPresent
capabilities of the provider, without having to resolve the provider to check for presence.daniel
11/09/2022, 9:11 PMisPresent
calculate the value to determine if there is a value. It’s a hidden problem that I discussed in length with the Gradle team but no conclusion yet. The problem is there is no way to know there is a value unless the value is calculated. Part of the problem is that null
is assumed to be no-value.Chris Lee
11/09/2022, 9:12 PMdaniel
11/09/2022, 9:19 PMdef i = 0
def p = provider {
try {
if (i == 0) {
return 'first'
} else if (i == 1) {
return null
} else {
return 'other'
}
} finally {
i++
}
}
if (p.present) {
println(p.getOrNull())
}
println p.getOrNull()
The result would be:
> Configure project :
null
other
I use getOrNull()
inside the condition because it would crash with no-value.Chris Lee
11/09/2022, 9:19 PMdaniel
11/09/2022, 9:20 PM