https://kotlinlang.org logo
Title
a

Andreas Sinz

03/10/2018, 4:32 PM
data class Foo private constructor (val a: Int, val b: Int)

fun Foo(a: Int, b: Int) = ...
s

Shawn

03/10/2018, 4:40 PM
it looks like that results in a conflicting override (and an accidental recursive call)
and if I change the method name, I still can’t access the private constructor from a top-level function
a

Andreas Sinz

03/10/2018, 4:41 PM
function and data class must be in the same file
s

Shawn

03/10/2018, 4:43 PM
they’re declared literally one after another in my code
a

Andreas Sinz

03/10/2018, 4:49 PM
nvm ^^
I get a runtime exception
so the best way is to use a
companion object
with
operator fun invoke
1
that should work with a private constructor
s

Shawn

03/10/2018, 4:59 PM
oh wow, that works pretty well 👍
a

Andreas Sinz

03/10/2018, 5:00 PM
yes, just make sure you override
copy
s

serg

03/10/2018, 5:32 PM
How do you "override" a generated function?
s

Shawn

03/10/2018, 5:33 PM
I’m not sure that you can - I got a platform declaration clash when I tried
a

Andreas Sinz

03/10/2018, 6:10 PM
https://kotlinlang.org/docs/reference/data-classes.html#data-classes
If there are explicit implementations of equals(), hashCode() or toString() in the data class body or final implementations in a superclass, then these functions are not generated, and the existing implementations are used
and
Deriving a data class from a type that already has a copy(...) function with a matching signature is deprecated in Kotlin 1.2 and will be prohibited in Kotlin 1.3.
so you can't override
copy()
or
componentN()
😕
s

Shawn

03/10/2018, 6:12 PM
so it goes 😕
a

Andreas Sinz

03/10/2018, 6:13 PM
TIL wasn't aware that you can't override all methods that are generated
s

serg

03/11/2018, 12:15 PM
In this case it would help just to prohibit (make private) the
copy()
method. I think it would make sense to give a finer grain control over generated methods.