https://kotlinlang.org logo
Title
a

aeruhxi

02/14/2018, 7:15 AM
data class Post(
        val id: String,
        val content: String,
        val timestamp: Long,
        val photos: List<String>
) : Model()

data class FullPost(
        val id: String,
        val content: String,
        val timestamp: Long,
        val photos: List<String>,
        val postedBy: User,
        val with: List<User>,
        val checkedIn: Restaurant,
        val actType: String,
        val likesCount: Int
) : Model()
Is there a better way to copy the fields of Post to FullPost? So that I don't have to repeat fields like this
c

Czar

02/14/2018, 7:25 AM
Nothing Kotlin-specific, but utils for Java in general are aplenty, choose whichever you like more, search for BeanUtil(s) copy properties or something like that.
o

Olekss

02/14/2018, 7:25 AM
You can use map and use property delegation to the map, and then copy map used by other class.
c

Czar

02/14/2018, 7:28 AM
basically either use a lib which can do this, or implement it yourself, e.g. map delegation, like Olekss proposed, reflection.
a

aeruhxi

02/14/2018, 7:37 AM
Any annotation based library for this that plays well with data class?
m

mzgreen

02/14/2018, 7:40 AM
why can’t you just make
FullPost
to extend
Post
?
a

aeruhxi

02/14/2018, 7:41 AM
data class can't be open
m

mzgreen

02/14/2018, 7:46 AM
Ah haven’t notice it. Maybe you can make
Model
to be a
sealed class
and move common fields there?
h

hallvard

02/14/2018, 7:48 AM
Is this pattern usable for you?
data class FullPost(val basisPost: Post
        val postedBy: User,
        val with: List<User>,
        val checkedIn: Restaurant,
        val actType: String,
        val likesCount: Int
) : Model()
a

aeruhxi

02/14/2018, 7:49 AM
nope
Many other classes extends
Model
. So can't really do that @mzgreen
I am checking out delegate by map
c

Czar

02/14/2018, 7:53 AM
Any annotation based library for this that plays well with data class?
You don't need annotations, seems pretty simple case. You could use something like Apache commons-beanutils
a

aeruhxi

02/14/2018, 8:00 AM
copyProperties
works on objects. I want it on classes. What I have in mind is something like
data class Post(
        val id: String,
        val content: String,
        val timestamp: Long,
        val photos: List<String>
) : Model()

@CopyProperties(Post::class)
data class FullPost(
        val postedBy: User,
        val with: List<User>,
        val checkedIn: Restaurant,
        val actType: String,
        val likesCount: Int
) : Model()
c

Czar

02/14/2018, 8:04 AM
Uh, then you have two solutions: code generation or byte code manipulation based on annotations. Research #kapt
a

aeruhxi

02/14/2018, 8:12 AM
thanks
m

marstran

02/14/2018, 8:36 AM
I think it will work quite nicely if you add delegation to the
basisPost
parameter in @hallvard's solution. It works only if
Model
is an interface though. Like this:
data class FullPost(val basisPost: Post
        val postedBy: User,
        val with: List<User>,
        val checkedIn: Restaurant,
        val actType: String,
        val likesCount: Int
) : Model() by basisPost
1
a

Andreas Sinz

02/14/2018, 8:38 AM
@aeruhxi you could use composition