Is it a bad idea to use `override val` in `data cl...
# getting-started
d
Is it a bad idea to use
override val
in
data class
? I want to give it a polymorphism to use these
data class
more flexible. For instance,
abstract class CalendarSelectedDate(
open val year: Int,
open val month: Int,
open val day: Int,
) {
open fun getDate(): String {
val calendar = Calendar.getInstance()._toTimeZero_()._apply_ *{*
set(year, month, day)
}
return _dateFormat_.format(calendar._time_)
}
}
class CalendarEndSelectedDate(
override val year: Int,
override val month: Int,
override val day: Int,
): CalendarSelectedDate(
year = year,
month = month,
day = day,
)
class CalendarStartSelectedDate(
override val year: Int,
override val month: Int,
override val day: Int,
): CalendarSelectedDate(
year = year,
month = month,
day,
)
I'm not sure it's good or not because it affects something bad on
equals()
and
copy()
the only thing i know is that
data class
is a class for holding the state. So, if i use
data class
and its`copy()` , Does that code violate the principles of a data class? + https://stackoverflow.com/questions/38492103/override-getter-for-kotlin-data-class
d
It can work, but I don't think this is a good design (for the example classes you gave).
It would make more sense to have an enum denoting the type:
Copy code
data class SelectedDate(val year: Int, val month: Int, val day: Int, val type: Type) {
   enum class Type {
      START,
      END;
   }
}
d
@Daniel Pitts Thank you Daniel! I think i have to use more
if
or
when
to clarify objects when i need to add new Type more and more if i use
enum class
in this case.
d
It kind of depends on what your needs are, but you can also add polymorphism to enum values.
d
@Daniel Pitts Thank you for sharing your insight :)
c
Overriding variables is a bit risky because it means you have two values. It's safer to either decide that the parent class holds the value:
Copy code
abstract class CalendarSelectedDate(
    val year: Int,
    val month: Int,
    val day: Int,
) { … }

class CalendarEndSelectedDate(
    year: Int,
    month: Int,
    day: Int,
) : CalendarSelectedDate(year, month, day)
or declare abstract variables to force the subclass to hold the value:
Copy code
abstract class CalendarSelectedDate {
    abstract val year: Int
    abstract val month: Int
    abstract val day: Int
}

class CalendarEndSelectedDate(
    override val year: Int,
    override val month: Int,
    override val day: Int,
) : CalendarSelectedDate()
2
In this specific case, I'm not sure what you're trying to do. If your goal is to have a type-safe difference between start and end date (so you can't assign one to the other accidentally), it would be clearer to avoid inheritance altogether and instead use:
Copy code
data class CalendarSelectedDate(
    val year: Int,
    val month: Int,
    val day: Int,
)

@JvmInline value class CalendarStartSelectedDate(val date: CalendarSelectedDate)
@JvmInline value class CalendarEndSelectedDate(val date: CalendarSelectedDate)
2
plus1 1