https://kotlinlang.org logo
Title
k

karelpeeters

04/18/2019, 6:27 PM
Not directly, you can make a top-level function next to the class with the same name to fake a constructor though. (or an
operator fun invoke
on the companion but that's even uglier)
j

Joe

04/18/2019, 6:28 PM
ok, seems like the other alternative is to do an unsafe cast in the identity transformer.... 🤷
r

Ruckus

04/18/2019, 6:30 PM
That would be very unsafe. What if I do
TableColumn<String, Int>("fred")
💥 1
👆 1
s

streetsofboston

04/18/2019, 7:17 PM
You could mitigate that by using a typealias:
data class TableColumn<T,U>(val rawColumn: T, val transformer: (U) ->T ) {
    constructor(rawColumn: T) : this(rawColumn, { it as T })
}

typealias TableColumnSame<T> = TableColumn<T, T>

fun main() {
    val tc = TableColumnSame(5)
}
Still quite unsafe, though, if you don’t use the typalias on the call site 🙂