Adrián
05/28/2019, 2:36 PMmax
05/28/2019, 2:43 PMcopy
method in the parent class, which takes all the parameters and gives them a default value, I have to check if it compiles toughStephan Schroeder
05/28/2019, 2:47 PMArkadii Ivanov
05/28/2019, 2:51 PMfun MyModel.copy(name: String = this.name, title: String = this.title): MyModel =
when (this) {
is MyModel.Model1 -> copy(name = name, title = title)
is MyModel.Model2 -> copy(name = name, title = title)
}
max
05/28/2019, 2:52 PMAdrián
05/28/2019, 3:11 PMAdrián
05/28/2019, 3:15 PMsealed class MyModel : DataClass {
abstract val name: String
abstract val title: String
}
With this, only Data classes could implement the Sealed class.Arkadii Ivanov
05/28/2019, 3:17 PMAdrián
05/28/2019, 3:40 PMArkadii Ivanov
05/28/2019, 3:43 PMAdrián
05/28/2019, 4:34 PMAdrián
05/28/2019, 4:35 PMfun MyModel.addEnvironmentToTitle(env: String) = copy(title = title + env)
Adrián
05/28/2019, 4:35 PMobobo
05/28/2019, 4:44 PMArkadii Ivanov
05/28/2019, 4:45 PMcopy
function for your particular sealed class and use it everywhere. You will have boilerplate code located in one place. You can then use it like MyMode.copy(title="whatever")
to change just title. When you add more entries later, you will just need to add them to the copy
method. It will be compile time safe.raulraja
05/28/2019, 5:29 PMsimon.vergauwen
05/28/2019, 5:30 PMsealed class
is meant to combine different types so in most cases the signatures wouldn’t match.Adrián
05/28/2019, 5:31 PMraulraja
05/28/2019, 5:31 PMAdrián
05/28/2019, 5:32 PMraulraja
05/28/2019, 5:33 PMAdrián
05/28/2019, 5:34 PMAdrián
05/28/2019, 5:34 PMsimon.vergauwen
05/28/2019, 5:35 PMtitle
property on the sealed class, meaning you don’t have it in all the subclasses you cannot update it without when
. This is were Optics could help you.
val titlePrism : Prism<SealedClass, String> = ...
titlePrism.modify(sealedClass) { title -> title + env }
That’ll update the title if the actual type has the title
property.Adrián
05/28/2019, 5:36 PMsimon.vergauwen
05/28/2019, 5:36 PMobobo
05/28/2019, 5:36 PMsimon.vergauwen
05/28/2019, 5:37 PMobobo
05/28/2019, 5:38 PMsealed data class
with the behavior you describe. I personally don't agree that this is necessary or desirable, but maybe other people do and this would find traction.Adrián
05/28/2019, 5:42 PMAdrián
05/28/2019, 5:47 PMAdrián
05/28/2019, 5:48 PMsimon.vergauwen
05/28/2019, 5:48 PMsimon.vergauwen
05/28/2019, 5:49 PM