Thomas Broyer
09/18/2026, 5:03 PMProperty<Boolean> would you rather initialize it with .convention(false) or mark it @Optional and use .getOrElse(false)? (or possibly both!)
Vampire
09/18/2026, 5:38 PM.convention(false) the question is 2- or 3-state, as you even then can unset the property.
If it is 2-state, I'd probably not use @Optional with getOrElse as that reduces cache hit rate, because the cache is from the property state, so you would segregate into three buckets while without @Optional it would only be two buckets.Thomas Broyer
09/18/2026, 7:10 PM@Optional + .getOrElse(false) either. ThanksVampire
09/18/2026, 7:18 PM@Optional for me implies it is three-state.TrevJonez
09/18/2026, 9:19 PMLaura Kassovic
09/20/2026, 8:37 PMLennart Jörelid
09/28/2026, 9:59 AM/**
* Creates a [Property] of type T within this extension.
*
* @param T The type of the property.
* @param defaultValue The default value of the property.
*/
protected fun <T : Any> optionalProperty(defaultValue: T): Property<T> =
PluginAlgorithms.optionalPropertyIn(project, defaultValue)
... which is backed by...
/**
* Creates a [Property] of type T within this extension.
*
* @param T The type of the property.
* @param defaultValue The default value of the property.
*/
@Suppress("UNCHECKED_CAST", "PLATFORM_CLASS_MAPPED_TO_KOTLIN")
internal fun <T : Any> optionalPropertyIn(project: Project, defaultValue: T): Property<T> {
val nonNullDefaultValue = Validation.validateNotNull(defaultValue, "defaultValue")
val nonNullProject = Validation.validateNotNull(project, "project")
val objectFactory = nonNullProject.objects
val toReturn = when (nonNullDefaultValue::class.java) {
Boolean::class.java, Boolean::class.javaObjectType, Boolean::class.javaPrimitiveType -> {
// Boolean requires special handling, since Gradle requires the value to be a java.lang.Boolean object.
val truthyDefaultValue: java.lang.Boolean = when (defaultValue.toString().toBoolean()) {
true -> java.lang.Boolean.TRUE as java.lang.Boolean
else -> java.lang.Boolean.FALSE as java.lang.Boolean
}
val booleanProvider = project.provider { truthyDefaultValue } as Provider<out java.lang.Boolean>
objectFactory.property(java.lang.Boolean::class.java)
.convention(booleanProvider)
}
else -> {
val tClass = nonNullDefaultValue::class.java as Class<T>
objectFactory.property(tClass)
.convention(nonNullDefaultValue)
}
}
return toReturn as Property<T>
}