Chris
03/16/2022, 3:58 PM/**
* Adds zero or more elements to the property value.
*
* <p>The given provider will be queried when the value of this property is queried.
* This property will have no value when the given provider has no value.
*
* @param provider Provider of elements
*/
void addAll(Provider<? extends Iterable<? extends T>> provider);
Is it really intended that if the Provider
passed to the *add*All is value-less that the entire list gets dropped (including anything previously add’ed or addAll’ed)?Tapchicoma
03/16/2022, 4:54 PMTapchicoma
03/16/2022, 4:55 PMChris
03/16/2022, 5:05 PMlist.convention([foo]);
list.add(bar);
should result in [foo, bar]
when it currently results in [bar]
only.
I’m saying that:
list.add(foo)
list.add(emptyProvider())
should result in [foo]
but actually results in []
.Vampire
03/16/2022, 5:48 PMNiels Doucet
03/17/2022, 9:01 AMVampire
03/17/2022, 9:21 AMset
where you set an empty provider, it is expected that the result is an empty provider.
But the point here is, that an add
or addAll
is called and it feels very wrong to me, that an add
or addAll
removes all content.Niels Doucet
03/17/2022, 9:26 AMadd
or addAll
you are interfering and telling the convention to back off. If you for some reason start adding nothing, that feels more like a user error.
This is very much a grey area and both sides have merit though 🤷Niels Doucet
03/17/2022, 9:29 AMVampire
03/17/2022, 9:29 AMVampire
03/17/2022, 9:30 AMVampire
03/17/2022, 9:31 AMNiels Doucet
03/17/2022, 10:17 AMtasks.register("listPropTest") {
doLast {
val list = objects.listProperty<String>()
list.add("test")
println("initial: ${list.get()}")
val otherList = objects.listProperty<String>()
otherList.addAll("one", "two", "three")
list.addAll(otherList)
println("with other: ${list.get()}")
val emptyList = objects.listProperty<String>()
list.addAll(emptyList)
println("with empty: ${list.get()}")
list.add(objects.property<String>())
println("with empty prop: ${list.get()}")
}
}
has output
> Task :listPropTest FAILED
initial: [test]
with other: [test, one, two, three]
with empty: [test, one, two, three]
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':listPropTest'.
> Cannot query the value of this property because it has no value available.
Not sure if that’s better than what’s described above 🤔Chris
03/17/2022, 2:31 PMdef list = project.objects.listProperty(String)
list.add('foo')
list.add(providers.gradleProperty('foo'))
if (list.isPresent()) {
println "List: ${list.get()}"
} else {
println 'List: <not-present>'
}
$ gradle -Pfoo=bar
List: [foo, bar]
$ gradle
List: <not-present>
Niels Doucet
03/17/2022, 2:49 PMVampire
03/17/2022, 3:09 PMlist.addAll(project.objects.listProperty(String).orElse([]))
Chris
03/17/2022, 3:12 PMNiels Doucet
03/17/2022, 3:14 PMisPresent()
method is delegating to any providers contained within the property, which does give you the impression that things have disappeared.Niels Doucet
03/17/2022, 3:15 PMVampire
03/17/2022, 3:20 PMaddAll
.
For add
it is not exactly as straightforward, as you need to add "something" from the provider, you cannot add "nothing" from the provider.Chris
03/17/2022, 3:28 PMChris
03/17/2022, 3:31 PMRepresents a property whose type is a {@link List} of elements of type {@link T}.
and the method starts:
Adds an element to the property value.
The add is not really adding, and the type is not really a list.Niels Doucet
03/17/2022, 3:36 PMNiels Doucet
03/17/2022, 3:37 PMisPresent
method to return an unintuitive resultChris
03/17/2022, 3:38 PMProvider<List<T>>
and not a Provider<List<Provider<List<T>>>
.Niels Doucet
03/17/2022, 3:41 PMlist [ entry1, provider { entry2 } ]
and you add an empty provider, the result is
list [ entry1, provider { entry2 }, provider { } ]
Niels Doucet
03/17/2022, 3:42 PMChris
03/17/2022, 3:44 PMList<Provider<List<T>>
or maybe a List<Provider<T>>
but not a Provider<List<T>>
Niels Doucet
03/17/2022, 3:45 PMProvider<List<T>>
, the fact that there’s lazy resolution involved is an internal implementation detail, which granted leads to confusing behaviour.Chris
03/17/2022, 3:48 PMNiels Doucet
03/17/2022, 3:52 PMNiels Doucet
03/17/2022, 3:52 PMChris
03/24/2022, 3:50 PM