From 2d714523412b80e9047d11194aa6b27ab73117b1 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Mon, 9 Dec 2019 20:56:09 +0300 Subject: [PATCH] Workaround for JS reflection for dimenstions --- .../kotlin/scientifik/kmath/dimensions/Dimensions.kt | 6 ++---- .../kotlin/scientifik/kmath/dimensions/Wrappers.kt | 10 ++++++++-- .../jsMain/kotlin/scientifik/kmath/dimensions/dim.kt | 5 +++++ .../jvmMain/kotlin/scientifik/kmath/dimensions/dim.kt | 4 ++++ .../scientifik/kmath/dimensions/DMatrixContextTest.kt | 0 5 files changed, 19 insertions(+), 6 deletions(-) create mode 100644 kmath-dimensions/src/jsMain/kotlin/scientifik/kmath/dimensions/dim.kt create mode 100644 kmath-dimensions/src/jvmMain/kotlin/scientifik/kmath/dimensions/dim.kt rename kmath-dimensions/src/{commonTest => jvmTest}/kotlin/scientifik/kmath/dimensions/DMatrixContextTest.kt (100%) diff --git a/kmath-dimensions/src/commonMain/kotlin/scientifik/kmath/dimensions/Dimensions.kt b/kmath-dimensions/src/commonMain/kotlin/scientifik/kmath/dimensions/Dimensions.kt index 87511b885..2eef69a0c 100644 --- a/kmath-dimensions/src/commonMain/kotlin/scientifik/kmath/dimensions/Dimensions.kt +++ b/kmath-dimensions/src/commonMain/kotlin/scientifik/kmath/dimensions/Dimensions.kt @@ -19,13 +19,11 @@ interface Dimension { } } } - - inline fun dim(): UInt { - return D::class.objectInstance?.dim ?: error("Dimension object must be a singleton") - } } } +expect inline fun Dimension.Companion.dim(): UInt + object D1 : Dimension { override val dim: UInt = 1u } diff --git a/kmath-dimensions/src/commonMain/kotlin/scientifik/kmath/dimensions/Wrappers.kt b/kmath-dimensions/src/commonMain/kotlin/scientifik/kmath/dimensions/Wrappers.kt index 9985888b9..c78018753 100644 --- a/kmath-dimensions/src/commonMain/kotlin/scientifik/kmath/dimensions/Wrappers.kt +++ b/kmath-dimensions/src/commonMain/kotlin/scientifik/kmath/dimensions/Wrappers.kt @@ -66,7 +66,8 @@ interface DPoint : Point { /** * Dimension-safe point wrapper */ -inline class DPointWrapper(val point: Point) : DPoint { +inline class DPointWrapper(val point: Point) : + DPoint { override val size: Int get() = point.size override fun get(index: Int): T = point[index] @@ -94,7 +95,12 @@ inline class DMatrixContext>(val context: GenericMatrixCon inline fun point(noinline initializer: (Int) -> T): DPoint { val size = Dimension.dim() - return DPoint.coerceUnsafe(context.point(size.toInt(), initializer)) + return DPoint.coerceUnsafe( + context.point( + size.toInt(), + initializer + ) + ) } inline infix fun DMatrix.dot( diff --git a/kmath-dimensions/src/jsMain/kotlin/scientifik/kmath/dimensions/dim.kt b/kmath-dimensions/src/jsMain/kotlin/scientifik/kmath/dimensions/dim.kt new file mode 100644 index 000000000..557234d84 --- /dev/null +++ b/kmath-dimensions/src/jsMain/kotlin/scientifik/kmath/dimensions/dim.kt @@ -0,0 +1,5 @@ +package scientifik.kmath.dimensions + +actual inline fun Dimension.Companion.dim(): UInt { + TODO("KClass::objectInstance does not work") +} \ No newline at end of file diff --git a/kmath-dimensions/src/jvmMain/kotlin/scientifik/kmath/dimensions/dim.kt b/kmath-dimensions/src/jvmMain/kotlin/scientifik/kmath/dimensions/dim.kt new file mode 100644 index 000000000..a16dd2266 --- /dev/null +++ b/kmath-dimensions/src/jvmMain/kotlin/scientifik/kmath/dimensions/dim.kt @@ -0,0 +1,4 @@ +package scientifik.kmath.dimensions + +actual inline fun Dimension.Companion.dim(): UInt = + D::class.objectInstance?.dim ?: error("Dimension object must be a singleton") \ No newline at end of file diff --git a/kmath-dimensions/src/commonTest/kotlin/scientifik/kmath/dimensions/DMatrixContextTest.kt b/kmath-dimensions/src/jvmTest/kotlin/scientifik/kmath/dimensions/DMatrixContextTest.kt similarity index 100% rename from kmath-dimensions/src/commonTest/kotlin/scientifik/kmath/dimensions/DMatrixContextTest.kt rename to kmath-dimensions/src/jvmTest/kotlin/scientifik/kmath/dimensions/DMatrixContextTest.kt