Olli Helenius
09/30/2022, 8:57 AMhfhbd
09/30/2022, 9:19 AMdata class C<T>(val t: T) {
fun copyGenerated(t: T = this.t): C<T> = C(t)
// your custom/expected copy method
fun<R> customCopy(t: R = this.t): C<R> = C(t) // does not work, because T has no relation with R
}
fun a() {
val c1 = C(t = "")
val c2 = c1.copyGenerated() // default t = this.t (String)
val c3 = c1.customCopy(t = 42) // usage works, because T is specified
val c4 = c1.customCopy() // error: R is unknown
}
But this does not work, because T has no relation with R, so type checking fails.Olli Helenius
09/30/2022, 9:35 AMhfhbd
09/30/2022, 10:12 AMOlli Helenius
09/30/2022, 10:15 AMKlitos Kyriacou
09/30/2022, 10:21 AMdata class C<T>(val t: T) {
fun copyGenerated(t: T = this.t): C<T> = C(t)
// your custom/expected copy method
fun<R> customCopy(t: R): C<R> = C(t) // works
fun customCopy(): C<T> = C(this.t) // also works
}
The problem is that when you have N arguments, where N could be large, you would need 2^N overloads, so this solution is not scalable.
It would be interesting to know how Scala implements this.Olli Helenius
09/30/2022, 10:25 AM"-Vprint:typer"
to Extra Sbt Configuration in the Scastie Build Settings tabOlli Helenius
09/30/2022, 10:28 AMcopy$default$1[A]
for the default values and passes those to each parameter to copy
that were not explicitly givenandylamax
09/30/2022, 3:13 PMmap
rather than copy
an object of type C<T>
should always return the same type when copied, but can return a different type upon a mapping