Skip to content

Remove redundant toType overloads with different nullability in convert API #1695

@koperagen

Description

@koperagen

So this causes an exception, as expected:

dataFrameOf("a" to columnOf(123, 322), "b" to columnOf(1.0, null)).convert { a and b }.to<Double>()

Method threw 'org.jetbrains.kotlinx.dataframe.exceptions.CellConversionException' exception.
I think this exception is the reason why two overloads for each operation were introduced:

public fun <T> Convert<T, Any>.toDouble(): DataFrame<T> = to<Double>()
public fun <T> Convert<T, Any?>.toDouble(): DataFrame<T> = to<Double?>()

But i want to argue it's actually redundant, public fun <T> Convert<T, Any?>.toDouble(): DataFrame<T> = to<Double?>() is enough:

If mixed nullability columns are selected, to<Double?>() is resolved

dataFrameOf("a" to columnOf(123, 322), "b" to columnOf(1.0, null)).convert { a and b }.toDouble()

And its result is accurate, a still has not-null Double type, just as originally it was Int.

a: Double
b: Double?

So in terms of type accuracy public fun <T> Convert<T, Any>.toDouble(): DataFrame<T> = to<Double>() is not needed.

Now let's return to the exception. If we deceive the compiler and resolve wrong overload, that normally wouldn't be resolved, then we get exception (that we might expect, since all our columns are Int! (not really))

dataFrameOf("a" to columnOf(123, 322), "b" to columnOf(1, null)).convert { (a and b).cast<Int>() }.toDouble()

Let's check other column selectors. all() -> ColumnSelector<Any?>, so again "nullable" overload is resolved.

dataFrameOf("a" to columnOf(123, 322), "b" to columnOf(1, null)).convert { all() }.toDouble()

Looks like we can remove ~27 overloads from convert.kt and everything will work same as before

Metadata

Metadata

Assignees

Labels

APIIf it touches our API

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions