It all started with a commit message: "kotlinx.android.synthetic is no longer a recommended practice. Removing in favour of explicit findViewById." The outpour of anger showed up in a Reddit thread on Android shortly afterwards, wondering why Google continues to release and remove new APIs, and with no explanation as to why this is no longer a recommended practice.
Exceptions are a common mean to handle errors. However, they have some drawbacks when it comes to compiler support, safety and traceability. Fortunately, we can leverage Kotlin’s sealed classes to create result objects that solve the mentioned problems. This way, we get great compiler support and the code becomes clean, less error-prone, easy to grasp and predictable.
Inspired by Discord now having spoiler tags, I decided to look into how this can be done. I was able to achieve a similar effect using SpannableString. So let’s get started.
My team recently cleaned up an API in an internal storage library. While it’s a simple case, it covers a couple interesting language tools and tradeoffs. Let’s dig in!