``` data class Post( val id: String, ...
# announcements
a
Copy code
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
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
You can use map and use property delegation to the map, and then copy map used by other class.
c
basically either use a lib which can do this, or implement it yourself, e.g. map delegation, like Olekss proposed, reflection.
a
Any annotation based library for this that plays well with data class?
m
why can’t you just make
FullPost
to extend
Post
?
a
data class can't be open
m
Ah haven’t notice it. Maybe you can make
Model
to be a
sealed class
and move common fields there?
h
Is this pattern usable for you?
Copy code
data class FullPost(val basisPost: Post
        val postedBy: User,
        val with: List<User>,
        val checkedIn: Restaurant,
        val actType: String,
        val likesCount: Int
) : Model()
a
nope
Many other classes extends
Model
. So can't really do that @mzgreen
I am checking out delegate by map
c
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
copyProperties
works on objects. I want it on classes. What I have in mind is something like
Copy code
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
Uh, then you have two solutions: code generation or byte code manipulation based on annotations. Research #C2R77UD35
a
thanks
m
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:
Copy code
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
@aeruhxi you could use composition