This message was deleted.
# plugin-development
s
This message was deleted.
👍 1
d
Your
ifPresent
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.
c
don’t believe that to be the case. it’s simply using the
isPresent
capabilities of the provider, without having to resolve the provider to check for presence.
d
isPresent
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.
c
uuuuuggggghhhhhh. thanks for flagging that.
d
It’s a gross approximation but given:
Copy code
def 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:
Copy code
> Configure project :
null
other
I use
getOrNull()
inside the condition because it would crash with no-value.
c
gotcha. thx.
👍 1
d
Granted, no-one should write a provider like that but if for example the provider would request something from the network and an exception occurs, it may result in that behaviour.