Fudge
05/21/2019, 6:48 AMthana
05/21/2019, 6:52 AMnull
has it's application and kotlin allows us to be aware of nullable values in a very clever way.
E.g. consider a Repository<Entity>
that has a fetchById(id: IdClass)
method.
what would you expect it to do? throw an exception if id
is unknown? Return FetchResult
instead of Entity
which would be dfined as
sealed class FetchResult {
object NoResult: FetcHresult
class SingleResult(val result: Entity): FetchResult
}
thana
05/21/2019, 6:53 AMEntity?
?dsavvinov
05/21/2019, 7:05 AMrook
05/21/2019, 2:08 PMrepo {
with(myObjId) {
//do work on MyObj-type
}
}
the repo interface looks like this
operator fun invoke(action: Repo.() -> Unit)
fun with(id: String, action: MyObj.() -> Unit)
Theoretically, you could establish a notFound
function parameter to define a different set of work in the case that the object could not be retrieved.