https://kotlinlang.org logo
#getting-started
Title
# getting-started
y

Youssef Shoaib [MOD]

03/16/2023, 10:28 AM
Any way to override a data class'
componentN
functions? This for instance throws error "Conflicting overloads: public final operator fun component1(): String defined in Foo":
Copy code
data class Foo(val bar: Int) {
    operator fun component1() = bar.toString()
}
s

Sam

03/16/2023, 10:34 AM
No — see https://kotlinlang.org/docs/data-classes.html
• Providing explicit implementations for the
componentN()
and
copy()
functions is not allowed.
But that restriction only applies to data classes. You can define your own
componentN
functions for any other class.
p

phldavies

03/16/2023, 10:35 AM
If it's just a single-property class you can use a value class and define a
component1()
👌 1
6 Views