louiscad
01/11/2018, 11:07 AMsealed class
inside another one is a bad idea? My use case is to represent the states of CameraCaptureSession
from Android Camera2 API (see these state callbacks: https://developer.android.com/reference/android/hardware/camera2/CameraCaptureSession.StateCallback.html)
Here's my updated sealed class
that is inside a custom CamCaptureSession
class:
sealed class State {
sealed class Configured() : State() {
companion object : Configured()
sealed class InputQueueEmpty : Configured() {
companion object : InputQueueEmpty()
object Ready : InputQueueEmpty()
}
object Active : Configured()
}
sealed class Closed : State() {
companion object : Closed()
object ConfigureFailed : Closed()
}
}
Czar
01/11/2018, 11:16 AMlouiscad
01/11/2018, 11:24 AMCzar
01/11/2018, 11:26 AMlouiscad
01/11/2018, 11:35 AMsealed class
raulraja
01/11/2018, 12:13 PMwhen
etc.copy
calls.louiscad
01/11/2018, 1:31 PMval stateDependentValue = when(sessionState) {
CamCaptureSession.State.Configured -> TODO()
CamCaptureSession.State.Configured.InputQueueEmpty -> TODO()
CamCaptureSession.State.Configured.InputQueueEmpty.Ready -> TODO()
CamCaptureSession.State.Configured.Active -> TODO()
CamCaptureSession.State.Closed.ConfigureFailed -> TODO()
CamCaptureSession.State.Closed -> TODO()
}
I could as well use is
to reduce the number of branches if I need to perform the same thing for any Configured
state for example.
About Optics from Arrow: I have a hard time understanding what is the purpose of it and in which use cases it could be used... is there a blog post or something to help demystify this?tschuchort
01/11/2018, 1:37 PMraulraja
01/11/2018, 2:38 PMcopy
does not scale well. The link I pasted about lenses has 2 sections Composition
and Generating Lenses
that show an example of the issue of having nested copy
methods and a solution with Lenses. Lenses are just functions with get
& set
that can be composed together into a new function that gives you back a copy of the object with your updated changes. Prisms are the same but to work with nested sealed hierarchies. I understand in your case you don't need it since it's a simple ADT for a specific purpose but thought I'd mention in any case since nesting sealed classes eventually becomes hard to maintain when attempting to update their nested values.radityagumay
01/12/2018, 2:14 PM