what's the functional (if any) difference between ...
# community-support
c
what's the functional (if any) difference between
Property::convention
and
Provider::orElse
since Property extends/implement Provider?
e
convention
modifies the
Property
,
orElse
returns a new
Provider
c
so, convention is preferred for making a default value?
seems like they'd achieve the same thing
e
Copy code
interface MyExtension {
    @get:Input
    val property: Property<String>
}
class MyPlugin : Plugin<Project> {
    override fun apply(project: Project) {
        val extension = project.objects.newInstance<MyExtension>()
        project.extensions.create("my", extension)
    }
}
Copy code
my.property.orElse("foo")
my.property.get() // error
Copy code
my.property.convention("foo")
my.property.get() // "foo"
on the other hand, maybe you do want
orElse
on the caller's side since even with a convention, a property can get unset
it depends
c
rubs temples
I think I want orElse, because what's the point of having a default if it can be un-defaulted...
I also think it doesn't matter because this is a convention plugin that I'm extracting from the copy-around pasta
v
The difference are other consumers of the property value. With
orElse
you say "when I here at this place want a value but the provider is empty, then use that value". With
convention
you say "if anyone wants to get the value of this property and it was not customized or was but then set to
null
, then that customer should get this value, no matter which consumer it is". But yes, even with a convention value one could set to an unset provider which makes the property unset even if a convention is set. Sometimes this even is intended and important. For example if some task has a property for a toolchain tool and a property for an executable and the tool is used if set, otherwise the executable, but the toolchain tool has a convention. Then you can set it to an empty provider, so that the executable property is used. So which to use, or maybe even both heavily depends on the individual use-case and intended behavior.