Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using Kotlin @Parcelize together with polymorphism

Hey I'm using one model as DTO, JsonCreator and Percelize my problem is that i want to use polymorphism (disassemble common part) and have no idea how to write it in Kotlin.

@Parcelize
@Entity
open class Location (var lat: Double = 0.0, var lng: Double = 0.0) :Parcelable

@Parcelize
@Entity
class MapsMarker(lat: Double, lng: Double, var name: String): Location(lat, lng), Parcelable

An issue here is that compiles shouts

Parcelizable constructor parameter should be "var or val"

for fields lat, lng of MapsMarker.

I'm using inheritance so I can't use val because I will override properties of the Location class. I also dont want my room @Entity will have duplicated fields.

If anyone knows the answer please help me ;)

like image 761
murt Avatar asked Nov 21 '17 11:11

murt


1 Answers

@Parcelize does not work well with inheritance issue link. You can still have custom parceler like mentioned here

<<<< not related to your question but consider giving a shot >>>>

Your MapsMarker class is violating is a relationship -> MapsMarker is a Location?

It should not inherit from Location instead prefer composition -> MapsMarker has a Location

@Parcelize
@Entity
class MapsMarker(val location: Location, var name: String): Parcelable

You can find many blogs detailing class designs. This is the first related google result I got now.

like image 122
Jegan Babu Avatar answered Oct 22 '22 17:10

Jegan Babu