https://kotlinlang.org logo
Title
g

gildor

05/22/2019, 3:47 PM
You can make
value
abstract field instead of constructor property, it's more flexible and less typing
r

Rok Koncina

05/22/2019, 3:48 PM
Android Studio is complaining that it can't be abstract, my guess is it doesn't work with sealed classes?
g

gildor

05/22/2019, 3:48 PM
It works, just make sealed class abstract and move property to body
s

Shawn

05/22/2019, 3:48 PM
no, it’s because the val is being declafred in the ctor
Sealed classes are already abstract
move the val out of the constructor and into the body, you’ll be all good
r

Rok Koncina

05/22/2019, 3:49 PM
you're right
r

Rok Koncina

05/22/2019, 3:51 PM
Yup, this is much better! Thanks guys! I still wish I didn't have to override it every time but ok...
s

streetsofboston

05/22/2019, 3:56 PM
Are you ever using
value
of a `BaseStatus`’s as-is, not cast to one of its sub-types?
g

gildor

05/22/2019, 11:16 PM
But how would it work if you do not override it? This solution allows you override it as constructor property or just as member property (including property with custom getter), so very flexible
r

Rok Koncina

05/23/2019, 8:57 AM
Yes, I'm using the value on some places without casting it. I could have it in a when but they all use the exact same value, it looks funny 🙂
I know it's pretty much impossible for it to work without specifying it in a subclass' constructor, but I was hoping Kotlin magic would provide something like
sealed class A(val value1: Type1)
data class B(val value2: Type2): A
and I could then initialise it with:
val b = B(Type2(), Type1())
I know this might be logically impossible, but hey, magic is magic 😉
g

gildor

05/23/2019, 9:26 AM
Yes, and this is implicit magic (already against general ideology of Kotlin) that also changes how inheritance work in the language for one specific case
r

Rok Koncina

05/23/2019, 9:51 AM
Exactly 🙂 And it's not such a specific case, most of the time when you're creating a subclass you want to add some more constructor parameters and you have to specify the same parameter(s) of the superclass and pass them through. It's a boilerplate code and Kotlin is so amazing at cleaning all the boilers that it spoiled me - whenever I write some boilerplate code in Kotlin my first though is: "There's probably some Kotlin trick that I don't know yet that cleans this" Well, not in this case I guess. (no complaints, ❤️ Kotlin!)