Awesome article, and great out of the box thinking. My only criticism here is you could avoid creating the extra companion object, the static method generated from using extension functions, and all the extra magic here, and replace it with just creating an abstract index/view type property on the parent sealed class. Then, all subclasses must override and provide their view type/index.
This has several advantages but the obvious ones are:
- You don’t risk introducing bugs in the event that adding/removing from your sealed class hierarchy breaks logic that was assuming certain view holders were before/after one another
- The concept of ordinal properties even from Enum class was never intended to be used in this way for reason #1.
- Much more lightweight in terms of code, logic introduced and readability with using just a single int property on the sealed class hierarchy rather than creating multiple unnecessary objects under the hood, in addition to reusable but sometimes difficult to read extension function with lambdas.
Regardless, nice work! It’s always a good thing to come up with some type of standard across the team that helps solve a reoccurring problem. 👍👋