https://kotlinlang.org logo
Title
g

ghedeon

03/18/2019, 3:31 PM
I remember a related kotlin discussion but any progress/plans to inherit arguments in a concise manner? This is awfully verbose:
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

ribesg

03/18/2019, 3:37 PM
Well you can start by removing
open
and
override val
g

ghedeon

03/18/2019, 3:43 PM
and trying to invent names that doesn't conflict with default ones? Like
title1
,
title2
? 😒imple_smile: I wouldn't call it an improvement.
g

gildor

03/18/2019, 3:54 PM
I prefer abstract properties in this case, doesn't require passing them to constructor or DiapogState
Not perfect, but imo better and already works
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

Mike

03/18/2019, 10:11 PM
@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

ghedeon

03/18/2019, 10:28 PM
data classes require properties, so it won't work. You'd need to content with regular classes and for me it's undesirable.
m

Mike

03/19/2019, 10:31 AM
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

gildor

03/19/2019, 10:32 AM
what about abstract property?
g

ghedeon

03/19/2019, 10:41 AM
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 😐