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)
}
}
}

See also

Types

Link copied to clipboard
class DeclaredParameter<out T>(val property: KProperty<*>, val argument: T)

A Parameter declared in a ParameterizeScope, providing access to the selected argument and enabling its use through property delegation.

Link copied to clipboard
value class Parameter<out T>(val arguments: Sequence<T>)

A sequence of arguments that can be declared within a ParameterizeScope to make one of the values available.

Functions

Link copied to clipboard
@JvmName(name = "parameterLazyIterable")
inline fun <T> ParameterizeScope.parameter(crossinline lazyArguments: LazyParameterScope.() -> Iterable<T>): ParameterizeScope.Parameter<T>
@JvmName(name = "parameterLazySequence")
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.

Used to declare a parameter with the supplied arguments.

Link copied to clipboard

Used to declare a parameter with the listed arguments.

Link copied to clipboard

Declares this Parameter, allowing one of its arguments to be used as the value of the Kotlin property.