From e039271367a15881136a5728f086d335427867cb Mon Sep 17 00:00:00 2001 From: Ben Woodworth Date: Sat, 11 Nov 2023 14:46:27 -0500 Subject: [PATCH] WIP - Immediately compute arguments --- src/commonMain/kotlin/ParameterState.kt | 2 +- src/commonMain/kotlin/ParameterizeState.kt | 10 ++-- src/commonTest/kotlin/ParameterStateSpec.kt | 17 +----- src/commonTest/kotlin/ParameterizeSpec.kt | 61 +-------------------- 4 files changed, 12 insertions(+), 78 deletions(-) diff --git a/src/commonMain/kotlin/ParameterState.kt b/src/commonMain/kotlin/ParameterState.kt index e0def5d..266ea70 100644 --- a/src/commonMain/kotlin/ParameterState.kt +++ b/src/commonMain/kotlin/ParameterState.kt @@ -163,7 +163,7 @@ internal class ParameterState<@Suppress("unused") out T> internal constructor() */ internal fun nextArgument() { val arguments = checkNotNull(arguments) { - "Cannot iterate arguments before parameter delegate has been declared" + "Cannot iterate arguments before parameter argument has been declared" } check(argument != Uninitialized) { diff --git a/src/commonMain/kotlin/ParameterizeState.kt b/src/commonMain/kotlin/ParameterizeState.kt index 3a8ccdc..fdb1b78 100644 --- a/src/commonMain/kotlin/ParameterizeState.kt +++ b/src/commonMain/kotlin/ParameterizeState.kt @@ -45,7 +45,7 @@ internal class ParameterizeState { if (it != null) throw ParameterizeException("Nesting parameters is not currently supported: `${property.name}` was declared within `${it.name}`'s arguments") } - val parameterIndex = parameterCount++ + val parameterIndex = parameterCount val parameter = if (parameterIndex in parameters.indices) { parameters[parameterIndex] @@ -57,8 +57,8 @@ internal class ParameterizeState { property.trackNestedUsage { parameter.parameterState.declare(property, arguments) } -// getParameterArgument(parameter, property) + parameterCount++ return parameter } @@ -94,10 +94,10 @@ internal class ParameterizeState { } /** - * Iterate the last parameter (by the order they're first used) that has a + * Iterate the last parameter (by order of when arguments are calculated) that has a * next argument, and reset all parameters that were first used after it * (since they may depend on the now changed value, and may be computed - * differently). + * differently). TODO Update * * Returns `true` if the arguments are at a new permutation. */ @@ -105,7 +105,7 @@ internal class ParameterizeState { var iterated = false val declaredParameterIterator = parameters - .listIterator(parameters.lastIndex + 1) + .listIterator(parameterCount) while (declaredParameterIterator.hasPrevious()) { val parameter = declaredParameterIterator.previous() diff --git a/src/commonTest/kotlin/ParameterStateSpec.kt b/src/commonTest/kotlin/ParameterStateSpec.kt index fb1589f..24530c7 100644 --- a/src/commonTest/kotlin/ParameterStateSpec.kt +++ b/src/commonTest/kotlin/ParameterStateSpec.kt @@ -118,18 +118,7 @@ class ParameterStateSpec { parameter.nextArgument() } - assertEquals("Cannot iterate arguments before parameter delegate has been declared", failure.message) - } - - @Test - fun next_before_initialized_should_throw_IllegalStateException() { - parameter.declare(::property, emptyList()) - - val failure = assertFailsWith { - parameter.nextArgument() - } - - assertEquals("Cannot iterate arguments before parameter argument has been initialized", failure.message) + assertEquals("Cannot iterate arguments before parameter argument has been declared", failure.message) } @Test @@ -209,10 +198,10 @@ class ParameterStateSpec { @Test fun redeclare_with_different_parameter_should_throw_ParameterizeException() { - parameter.declare(::property, emptyList()) + parameter.declare(::property, listOf(Unit)) assertFailsWith { - parameter.declare(::differentProperty, emptyList()) + parameter.declare(::differentProperty, listOf(Unit)) } } diff --git a/src/commonTest/kotlin/ParameterizeSpec.kt b/src/commonTest/kotlin/ParameterizeSpec.kt index bb910df..9b7c435 100644 --- a/src/commonTest/kotlin/ParameterizeSpec.kt +++ b/src/commonTest/kotlin/ParameterizeSpec.kt @@ -196,52 +196,7 @@ class ParameterizeSpec { } @Test - fun parameters_that_are_used_out_of_order_should_iterate_in_declaration_order() { - // Because they should iterate in the order their arguments are calculated, and that happens at declaration. - - // Parameters that (potentially) depend on another parameter should iterate first, since if it were the other - // way around, and the depended on parameter iterates first, the dependent parameter's argument would be based - // on a now changed value and wouldn't be valid. - - // Basic test - testParameterize( - listOf("a1", "a2", "b1", "b2") - ) { - val first by parameterOf("a", "b") - val second by parameterOf(1, 2) - - useParameter(second) - useParameter(first) - - "$first$second" - } - - // More contrived test - testParameterize( - listOf("a1", "b1", "b2", "c1", "c2", "c3") - ) { - var secondCount = 1 // Based on first: a=1, b=2, ... - - class SecondIterator : Iterator { - var nextInt: Int = 1 - override fun hasNext(): Boolean = nextInt <= secondCount - override fun next(): Int = nextInt++ - } - - val first by parameterOf('a', 'b', 'c') - val second by parameter(Iterable(::SecondIterator)) - - useParameter(second) - useParameter(first) - - secondCount = first - 'a' + 1 - - "$first$second" - } - } - - @Test - fun parameters_that_are_used_out_of_order_should_iterate_in_declaration_order_contrived() = testParameterize( + fun parameters_that_are_used_out_of_order_should_iterate_in_declaration_order() = testParameterize( listOf("a1", "a2", "b1", "b2") ) { // Because they should iterate in the order their arguments are calculated, and that happens at declaration. @@ -250,22 +205,12 @@ class ParameterizeSpec { // way around, and the depended on parameter iterates first, the dependent parameter's argument would be based // on a now changed value and wouldn't be valid. - var secondCount = 1 // Based on `first`: a=1, b=2, ... - - class SecondIterator : Iterator { - var nextInt: Int = 1 - override fun hasNext(): Boolean = nextInt <= secondCount - override fun next(): Int = nextInt++ - } - - val first by parameterOf('a', 'b', 'c') - val second by parameter(Iterable(::SecondIterator)) + val first by parameterOf("a", "b") + val second by parameterOf(1, 2) useParameter(second) useParameter(first) - secondCount = first - 'a' + 1 - "$first$second" }