Skip to content

Instantly share code, notes, and snippets.

@sliskiCode
Last active January 11, 2018 12:56
Show Gist options
  • Save sliskiCode/429941e7330dac45babf305b40f8ec78 to your computer and use it in GitHub Desktop.
Save sliskiCode/429941e7330dac45babf305b40f8ec78 to your computer and use it in GitHub Desktop.
6 magic sugars that can make your Kotlin codebase happier #18
class MediaItemRenderer: Renderer {
override fun render(view: View, item: Item) = with(view) {
withCorrectType<MediaItem>(item) {
show { it.media() }
reset()
}
}
}
class IconItemRenderer: Renderer {
override fun render(view: View, item: Item) = with(view) {
withCorrectType<IconItem>(item) {
clear()
show { it.icon() }
}
}
}
inline fun <reified T> withCorrectType(toBeChecked: Item, block: (T) -> Unit) {
if (toBeChecked !is T) {
throw IllegalArgumentException("Invalid type, should be ${T::class.java.simpleName}")
}
block.invoke(toBeChecked)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment