Jaewoong Eum
1 min readMay 10, 2020

--

Hi, thank you for having an interest in this post.
Yes, I agree. We can write Add branches easily using the IDE.
But from the perspective of extension and scope function, sometimes it makes some benefits for reducing codes.

Definitely, the when clause is easy to read and understandable but we must get the fields of the response from full expression.

when (response) {
is ApiResponse.Success -> {
liveDate.postValue(response.data)
}
}

And here is the onSuccess scoped extension function.

response.onSuccess {
liveDate.postValue(data)
}

Actually, the example just used data only once, but if we have to use many more fields of the response, we have to write more full expression codes.

This extension is similar to the apply, let or with scope functions (has a receiver and we can use it in the lambda unit).

As a result, those onSuccess, onFailure, onException extensions are the same expression of the ApiResponse + apply scope function for accessing fields more quickly.

--

--

Jaewoong Eum
Jaewoong Eum

Written by Jaewoong Eum

Senior Android Developer Advocate @ Stream 🥑 • GDE for Android • OSS engineer. Love psychology, magic tricks, and writing poems. https://github.com/skydoves

No responses yet