Last active
January 11, 2018 12:56
-
-
Save sliskiCode/429941e7330dac45babf305b40f8ec78 to your computer and use it in GitHub Desktop.
6 magic sugars that can make your Kotlin codebase happier #18
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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