ParameterizeScope
The scope for parameterized code blocks, exposing parameter builders and enabling them to be declared.
Delegating to this interface makes it possible to observe parameter declaration, introspect arguments, or even transform/augment the parameter that's passed to the underlying scope in order to modify their behavior.
As an example, a testing framework could take advantage of this to run a specific set of arguments and track each of their values:
private class PickingParameterizeScope(
private val parameterizeScope: ParameterizeScope,
private val pickArgumentIndices: List<Int>
) : ParameterizeScope {
private var parameterIndex = 0
// Intercept the parameter, and declare it with just the one picked argument
override fun <T> Parameter<T>.provideDelegate(thisRef: Nothing?, property: KProperty<*>): DeclaredParameter<T> {
val pickArgumentIndex = pickArgumentIndices.getOrNull(parameterIndex++)
?: throw IllegalArgumentException("Argument index not specified for ${property.name}")
val pickedArgument = arguments.drop(pickArgumentIndex).elementAtOrNull(1)
?: throw IllegalArgumentException("Parameter ${property.name} has no argument at index $pickArgumentIndex")
return parameterizeScope.run {
parameterOf(pickedArgument).provideDelegate(thisRef, property)
}
}
}Content copied to clipboard
See also
Types
Link copied to clipboard
A Parameter declared in a ParameterizeScope, providing access to the selected argument and enabling its use through property delegation.
Functions
Link copied to clipboard
inline fun <T> ParameterizeScope.parameter(crossinline lazyArguments: LazyParameterScope.() -> Iterable<T>): ParameterizeScope.Parameter<T>
inline fun <T> ParameterizeScope.parameter(crossinline lazyArguments: LazyParameterScope.() -> Sequence<T>): ParameterizeScope.Parameter<T>
Used to declare a parameter with the computed lazyArguments. The arguments are only computed the first time the parameter is used, and not at all if used.
Link copied to clipboard
Link copied to clipboard
abstract operator fun <T> ParameterizeScope.Parameter<T>.provideDelegate(thisRef: Nothing?, property: KProperty<*>): ParameterizeScope.DeclaredParameter<T>