From 92cb088f36ab3ba24e5bb1d4b6bb8e6cbb547aaa Mon Sep 17 00:00:00 2001 From: sergeych Date: Sun, 21 Dec 2025 22:49:35 +0100 Subject: [PATCH] lazy messages for check/require --- docs/Testing.md | 15 +++++++++++++++ lynglib/build.gradle.kts | 2 +- .../commonMain/kotlin/net/sergeych/lyng/Script.kt | 10 ++++++---- lynglib/src/commonTest/kotlin/ScriptTest.kt | 10 ++++++++++ 4 files changed, 32 insertions(+), 5 deletions(-) diff --git a/docs/Testing.md b/docs/Testing.md index 51ad00d..b344293 100644 --- a/docs/Testing.md +++ b/docs/Testing.md @@ -87,8 +87,23 @@ While not strictly for testing, these functions help in defensive programming: Throws an `IllegalArgumentException` if the condition is false. Use this for validating function arguments. +If we want to evaluate the message lazily: + + require(condition) { "requirement not met: %s"(someData) } + +In this case, formatting will only occur if the condition is not met. + + + ### `check` check(condition, message="check failed") Throws an `IllegalStateException` if the condition is false. Use this for validating internal state. + +With lazy message evaluation: + + check(condition) { "check failed: %s"(someData) } + +In this case, formatting will only occur if the condition is not met. + diff --git a/lynglib/build.gradle.kts b/lynglib/build.gradle.kts index bbc4d9f..850526b 100644 --- a/lynglib/build.gradle.kts +++ b/lynglib/build.gradle.kts @@ -21,7 +21,7 @@ import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl import org.jetbrains.kotlin.gradle.dsl.JvmTarget group = "net.sergeych" -version = "1.0.9-SNAPSHOT" +version = "1.0.10-SNAPSHOT" // Removed legacy buildscript classpath declarations; plugins are applied via the plugins DSL below diff --git a/lynglib/src/commonMain/kotlin/net/sergeych/lyng/Script.kt b/lynglib/src/commonMain/kotlin/net/sergeych/lyng/Script.kt index a3df3e6..e84973c 100644 --- a/lynglib/src/commonMain/kotlin/net/sergeych/lyng/Script.kt +++ b/lynglib/src/commonMain/kotlin/net/sergeych/lyng/Script.kt @@ -253,16 +253,18 @@ class Script( addFn("require") { val condition = requiredArg(0) if (!condition.value) { - val message = args.list.getOrNull(1)?.toString() ?: "requirement not met" - raiseIllegalArgument(message) + var message = args.list.getOrNull(1) + if( message is Statement ) message = message.execute(this) + raiseIllegalArgument(message?.toString() ?: "requirement not met") } ObjVoid } addFn("check") { val condition = requiredArg(0) if (!condition.value) { - val message = args.list.getOrNull(1)?.toString() ?: "check failed" - raiseIllegalState(message) + var message = args.list.getOrNull(1) + if( message is Statement ) message = message.execute(this) + raiseIllegalState(message?.toString() ?: "check failed") } ObjVoid } diff --git a/lynglib/src/commonTest/kotlin/ScriptTest.kt b/lynglib/src/commonTest/kotlin/ScriptTest.kt index e2e0159..c3bd475 100644 --- a/lynglib/src/commonTest/kotlin/ScriptTest.kt +++ b/lynglib/src/commonTest/kotlin/ScriptTest.kt @@ -4297,4 +4297,14 @@ class ScriptTest { assertEquals(tf(...{ x: 3, y: 4, z: 50 }), "x=3, y=4, z=50") """.trimIndent()) } + + +// @Test +// fun testSplatAssignemnt() = runTest { +// eval(""" +// val abc = [1, 2, 3] +// val [a, b, c] = ...abc +// println(a, b, c) +// """.trimIndent()) +// } }