https://kotlinlang.org logo
Title
m

menegatti

07/10/2017, 7:33 AM
would an interface with those 3 fields work for you?
n

nayanjyoti

07/10/2017, 7:54 AM
menegatti: I was hoping of doing it in another way.
interfarce would be too clumsy as override is needed in both
m

marstran

07/10/2017, 8:19 AM
You could use delegation to do something like this:
interface A {
    val fieldA: String
    val fieldB: String
    val fieldC: String
}

data class FirstClass(
        override val fieldA: String, 
        override val fieldB: String, 
        override val fieldC: String) : A

data class SecondClass(val firstClass: FirstClass, val fieldD: String) : A by firstClass
n

nayanjyoti

07/10/2017, 9:16 AM
The extra interface declaration is bugging me. if there was any other way..
m

menegatti

07/10/2017, 9:17 AM
there isn't any
really, if you're just trying to prevent typing the parameters use multi-cursor or have template to use
if the objects share no behavior and you don't want to have a base class, then there's nothing the language can help you with
m

marstran

07/10/2017, 9:27 AM
If the classes are conceptually related, I think it really does make sense to extract that interface.
But if they just coincidentally have the same fields, don't do it.
☝️ 2
n

nayanjyoti

07/10/2017, 9:40 AM
They are request-response for a particular web service, and i find it not suitable to extend an interface. Currently, I ended up copying the same fields to the other classes as well. the other class does have few extra fields as well.
m

menegatti

07/10/2017, 9:40 AM
then maybe they are related
if all (or most of) your requests have the same fields
then they share behavior and the interface becomes the right modelling there
n

nayanjyoti

07/10/2017, 9:50 AM
I meant these two were the request and response for one webservice. the other requests and response don’t share the similar behaviour.
m

menegatti

07/10/2017, 10:03 AM
I see