Hey! Another Providers-related question: I'm addin...
# community-support
j
Hey! Another Providers-related question: I'm adding lazy dependencies to the configuration, like:
Copy code
configurations[configurationName].dependencies.addLater(
    myProvider.map { myValue ->
        println("myValue=$myValue")
        return ...
    }
)
And yes, this gets printed 20-30 times when the project is being configured. Is that expected, and I shouldn't interfere with that, or is using a cached provider a good choice here?
v
It is expected. A
map
on a provider (unless
finalizeValue()
or
finalizeValueOnRead()
is used at an appropriate place) is called each time the provider is queried. So if this gets printed 20-30 times, the provider is queried 20-30 times. If what you do is stable and fast, you should probably not care too much. "cached provider" are unfortunately not existing yet. "cached provider" or maybe better "memoized provider" are requested by me in https://github.com/gradle/gradle/issues/25550 together with a way to "fake" them right now for example for expensive calculations.
j
I already have a cachedProvider present and in use:
Copy code
internal inline fun <reified T : Any> cachedProvider(
    objects: ObjectFactory,
    providers: ProviderFactory,
    crossinline value: () -> T,
) = objects
    .property<T>()
    .value(providers.provider { value() })
    .apply {
        disallowChanges()
        finalizeValueOnRead()
    }
but I am reviewing all the code now and started wondering if regular providers (and zips) shouldn't be used instead.
v
Ah, I think I gave you that link already previously, looks very much like an adaption of my code 😄
j
Some of those dependencies read from the file system or perform other calculations.
Yes, that comes from your previous ideas which work great. 🙂
v
My examples in the issue now also support
List
,
Map
, and
Set
, and there is now also
cachedZip
🙂