September 2023
Intermediate to advanced
470 pages
11h 12m
English
We can now rewrite the readTextFile and getPersonDetails methods of the previous greeting email example using an Outcome:
| | fun readTextFile(filename: String): Outcome<String> = |
| | if (fileExists && canRead) |
| | readfile(File(filename)).asSuccess() |
| | else |
| | "$filename not found".asFailure() |
| | |
| | |
| | fun getPersonDetails(nickname: String): Outcome<Person> { |
| | |
| | val resp = remoteRead(nickname) |
| | |
| | return if (resp.status == OK) |
| | Person.parse(resp.bodyString()).asSuccess() |
| | else if (resp.status = NOT_FOUND ) |
| | "$nickname not found!".asFailure() |
| | else |
| | resp.bodyString().asFailure() |
| | } |
Note that Person.parse and readfile can also fail, and it would be better to make them return an Outcome as well.
So far, ...
Read now
Unlock full access