Leon K
12/05/2019, 4:17 PMsean
12/05/2019, 4:26 PMLeon K
12/05/2019, 4:41 PMsean
12/05/2019, 4:47 PMLeon K
12/05/2019, 6:40 PMelizarov
12/06/2019, 6:50 AMLeon K
12/06/2019, 4:23 PMval myValue = when(sealedResult) {
is MyResult.Ok -> sealedResult.value
is MyResult.Err -> sealedResult.errorValue
}
is still okay, (but a bit anoyying), but as soon as you need to reach into more nested structures or need to match on multiple sealed classes, this becomes hard to write and read. imagine the following:
val myValue = match(loggingConfig, IntesomeWebResult) {
_, WebResult.Ok(myData) -> ...
_, WebResult.Loading -> ...
LoggingConfig.Ignore, _ -> {}
LoggingConfig.WriteOnlyFatal(logFile), WebResult.Err(Error.Fatal(msg)) -> logFile.appendLog(msg)
LoggingConfig.WriteAll(logFile), WebResult.Err(error) -> logFile.appendLog(error.msg)
LoggingConfig.PrintLogs, WebResult.Err(error) -> println(error.msg)
}
i know, this is a pretty bad example (i'm rather uncreative atm), but it does show a neat usecase for pattern-matching in complex situations. it makes it easy to see under which circumstances things are ignored or used, without having to look at long property-access-chains or complex, nested if statementsraulraja
12/08/2019, 10:46 PMwhen (result) {
WebResult.ok.data -> ...
}
You can already do this kind of pattern matching with prisms and lenses in arrow today <https//arrow kt.io/docs/optics/dsl/|https//arrow-kt.io/docs/optics/dsl/>tmg
01/03/2020, 10:16 AM