I remember a related kotlin discussion but any pro...
# announcements
g
I remember a related kotlin discussion but any progress/plans to inherit arguments in a concise manner? This is awfully verbose:
Copy code
sealed class DialogState(open val title: String, open val text: String) {
    data class ConfirmDelete(override val title: String, override val text: String) : DialogState(title, text)
    data class CantDelete(override val title: String, override val text: String) : DialogState(title, text)
}
r
Well you can start by removing
open
and
override val
g
and trying to invent names that doesn't conflict with default ones? Like
title1
,
title2
? simple smile I wouldn't call it an improvement.
g
I prefer abstract properties in this case, doesn't require passing them to constructor or DiapogState
Not perfect, but imo better and already works
Copy code
sealed class DialogState { 
   abstract val title: String
   abstract val text: String

    data class ConfirmDelete(override val title: String, override val text: String) : DialogState()
    data class CantDelete(override val title: String, override val text: String) : DialogState()
}
Also this allow to avoid unnecessary field when you want to use getter instead of property in one or child classes
m
@ghedeon if you follow @ribesg suggestion, you don't need to invent names. The subclasses take a parameter, rather than defining a property by removing override val. Just have ConfirmDelete(title:String, text:String): DialogState(title, text). And DialogState only needs val on property to expose in all. No need for override. No need for unique names as the function parameter doesn't conflict with the inherited property.
g
data classes require properties, so it won't work. You'd need to content with regular classes and for me it's undesirable.
m
Ahhh. Missed that detail. Sorry about that. Thus far I haven't needed a sealed class to be a data class as I've never compared them or used them as keys in a map or similar.
g
what about abstract property?
g
abstract property is what I ended up using, it still boggles my mind that I type the same names in 3 places instead of one. Was trying to see what other languages do, but didn't find an example of a better syntax to propose 😐