Kotlin / dataframe

Structured data processing in Kotlin
https://kotlin.github.io/dataframe/overview.html
Apache License 2.0
761 stars 48 forks source link

`"" + columnRef` becomes `String` #750

Open sorokod opened 1 week ago

sorokod commented 1 week ago

Sample code (version 0.12.0):

    val df1 = dataFrameOf("name", "age")(
        "Alice", 15,
        "Bob", 20,
        "Charlie", 100
    )

    val name  by column<String>()
    val name_age by column<String>()

    df1.add(name_age) { name + "!!!" }.also { println(it) }
    df1.add(name_age) { "!!!" + name  }.also { println(it) }

The first println (name + "!!!") produces:

      name age   name_age
 0   Alice  15   Alice!!!
 1     Bob  20     Bob!!!
 2 Charlie 100 Charlie!!!

The second println ("!!!" + name) produces:

      name age                                 name_age
 0   Alice  15 !!!org.jetbrains.kotlinx.dataframe.im...
 1     Bob  20 !!!org.jetbrains.kotlinx.dataframe.im...
 2 Charlie 100 !!!org.jetbrains.kotlinx.dataframe.im...

The root cause is that In the two example + resolves to different functions. A foot gun that should be fixed.

Jolanrensen commented 1 week ago

It should indeed be fixed but unfortunately we can't. It's a language limitation.

Have you tried making a function like operator fun String.plus(something)? image Since String already has a function plus, an extension function overload will never be called by default.

sorokod commented 6 days ago

I would consider deprecating, and ultimately removing:

public operator fun ColumnReference<String>.plus.

Jolanrensen commented 6 days ago

Might indeed be a good idea to add ColumReference<String>.append() and .prepend() or something similar and through deprecations steer users to those :) We still cannot prevent "" + anything from becoming a String though