Last active
August 9, 2019 16:53
-
-
Save junlarsen/2eb960de69007ff63c3e43d1dcce9758 to your computer and use it in GitHub Desktop.
This file contains 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 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