1
1
package org.jetbrains.kotlinx.dataframe.impl.columns
2
2
3
+ import org.jetbrains.kotlinx.dataframe.BuildConfig
3
4
import org.jetbrains.kotlinx.dataframe.DataColumn
4
5
import org.jetbrains.kotlinx.dataframe.api.dataFrameOf
6
+ import org.jetbrains.kotlinx.dataframe.impl.isArray
7
+ import org.jetbrains.kotlinx.dataframe.impl.isPrimitiveArray
8
+ import kotlin.reflect.KClass
5
9
import kotlin.reflect.KType
10
+ import kotlin.reflect.full.isSubclassOf
6
11
7
12
internal abstract class DataColumnImpl <T >(
8
13
protected val values : List <T >,
@@ -12,6 +17,31 @@ internal abstract class DataColumnImpl<T>(
12
17
) : DataColumn<T>,
13
18
DataColumnInternal <T > {
14
19
20
+ private infix fun <T > T?.matches (type : KType ) =
21
+ when {
22
+ this == null -> type.isMarkedNullable
23
+
24
+ this .isPrimitiveArray ->
25
+ type.isPrimitiveArray &&
26
+ this !! ::class .qualifiedName == type.classifier?.let { (it as KClass <* >).qualifiedName }
27
+
28
+ this .isArray -> type.isArray
29
+
30
+ // cannot check the precise type of array
31
+ else -> this !! ::class .isSubclassOf(type.classifier as KClass <* >)
32
+ }
33
+
34
+ init {
35
+ // Check for [Issue #713](https://github.com/Kotlin/dataframe/issues/713).
36
+ // This only runs with `kotlin.dataframe.debug=true` in gradle.properties.
37
+ if (BuildConfig .DEBUG ) {
38
+ require(values.all { it matches type }) {
39
+ val types = values.map { if (it == null ) " Nothing?" else it!! ::class .simpleName }.distinct()
40
+ " Values of column '$name ' have types '$types ' which are not compatible given with column type '$type '"
41
+ }
42
+ }
43
+ }
44
+
15
45
protected val distinct = distinct ? : lazy { values.toSet() }
16
46
17
47
override fun name () = name
0 commit comments