Skip to content

Instantly share code, notes, and snippets.

@junlarsen
Last active August 9, 2019 16:53
Show Gist options
  • Save junlarsen/2eb960de69007ff63c3e43d1dcce9758 to your computer and use it in GitHub Desktop.
Save junlarsen/2eb960de69007ff63c3e43d1dcce9758 to your computer and use it in GitHub Desktop.
class A {
fun hello() = "hello"
}
class Container {
val container: MutableMap<String, Any> = mutableMapOf()
fun add(label: String, item: Any) {
container.put(label, item)
}
inline fun <reified T> get(item: String): T {
val res = container[item]
return if (res is T) res
else throw RuntimeException("Item type and name did not match any records.")
}
}
fun main() {
val container = Container()
container.add("a", A())
val a = container.get<A>("a")
println(a.hello())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment