Colton Idle
08/14/2020, 6:58 AMwhen (it) {
is EventDriver.Stop -> ...
is EventDriver.Go -> ...
}
and
when (it) {
EventDriver.Stop -> ...
EventDriver.Go -> ...
}
seem to compile and run fine. What's the difference? I would have expected is
to be required here?phldavies
08/14/2020, 7:31 AMColton Idle
08/14/2020, 8:01 AMphldavies
08/14/2020, 8:29 AMsealed class EventDriver {
sealed class Go: EventDriver() {
object Forward: Go()
object Backward: Go()
}
object Stop: EventDriver()
}
Then you would either want to do:
when(it) {
EventDriver.Stop -> ...
EventDriver.Go.Forward -> ...
EventDriver.Go.Backward -> ...
}
or
when(it) {
is EventDriver.Go -> ...
EventDriver.Stop -> ...
}
is
in that case would be superfluousColton Idle
08/14/2020, 1:51 PMis
when working with sealed classes and when
statementnanodeath
08/14/2020, 2:07 PMColton Idle
08/14/2020, 3:07 PMbrandonmcansh
08/14/2020, 9:21 PMis
and give you direct access to its members.when (it) {
is EventDriver.Go -> accelerate(it.mph)
is EventDriver.Stop -> stop(it.car)
}