Description
Kotlin stdlib uses lambdas to build different types of complex objects:
buildString
buildSequence
List builder
I propose to add intent builder to ktx. That will work like:
buildIntent {
action = "some.Action"
data = "http://example.com".toUri()
putExtra("name", value)
}
Possible implementation:
inline fun buildIntent(initializer: Intent.() -> Unit) = Intent().apply { initializer() }
Also we can move often used properties to arguments of the function, such as action:
inline fun buildIntent(action: String) = Intent(action).apply { initializer() }
but not sure that this is make a lot of sense.
But another feature would more useful. Intent builder with reified generic instead class argument:
inline fun <reified T> buildIntent(
context: Context,
initializer: Intent.() -> Unit
) = Intent(context, T::class.java).apply { initializer() }
This version also can provide empty lambda as default argument for initilizer, to make builder usage optional, reified acitivity creation already useful feature
Possible naming:
Intent
(same as List
builder, that looks like constructor call but actually function)
buildIntent
(same as stringBuilder
)
intent
is short but doesn't follow current Kotlin naming and clashes with variable names