bloder
09/02/2019, 12:43 PMMehdi
09/02/2019, 4:36 PMbloder
09/02/2019, 5:06 PMval x = either("1")
when(x) {
is Either.Left -> when(x.a) {
is Error.ErrorA -> "Error A"
is Error.ErrorB -> "Error B"
}
is Either.Right -> "Success"
}
bloder
09/02/2019, 5:09 PM...
is Error.ErrorC -> "Error C"
...
bloder
09/02/2019, 5:14 PM...
x = either("1")
handleEither(x)
}
private fun handleEither(@When either: Either.Right) = println("Success")
private fun handleEither(@When either: Either.Left) {
println("Handling Error...")
handleError(either.a)
}
private fun handleError(@When error: Error.ErrorA) = println("Error A")
private fun handleError(@When error: Error.ErrorB) = println("Error B")
bloder
09/02/2019, 5:16 PMprivate fun handleError(@When error: Error.ErrorC) = println("Error C")
jmfayard
09/03/2019, 7:35 AMMotivation
is misleading, no else ->
branch is needed in Kotlin with a sealed classpurfakt
09/03/2019, 9:57 AMwhen
?bloder
09/03/2019, 12:33 PMDico
09/03/2019, 8:46 PMDico
09/03/2019, 8:46 PMbloder
09/03/2019, 9:48 PMfun run(action: Action) = when(action) {
is Action.Success -> { // handle success }
is Action.ErrorA -> { // handle error a }
is Action.ErrorB -> { // handle error b }
is Action.ErrorC -> { // handle error c }
...
is Action.ErrorZ -> { // handle error z }
}
bloder
09/03/2019, 9:54 PMfun run(action: Action) = applyAction(action)
fun applyAction(@When Action.Success) {} // handle success
fun applyAction(@When Action.ErrorA) {} // handle error a
fun applyAction(@When Action.ErrorB) {} // handle error b
fun applyAction(@When Action.ErrorC) {} // handle error c
...
fun applyAction(@When Action.ErrorZ) {} // handle error z
Dico
09/03/2019, 10:39 PMbloder
09/03/2019, 11:13 PMvery determinable place
, and what I mean by extension is actually creating new structs to handle solutions instead of lines of code in a struct (and I understand your point about consider a function inside a class as not a struct but a simple line of code but if it's considered probably we'd have a lot of verbose and complex solutions), in a real world example we'd have verbose and big functions just to type check and call its handling, instead of separate more all responsibilities by creating shorter structs.Dico
09/04/2019, 11:47 AM