-
-
Save emrul/b25edd2a78a141b031731cbec85c9f50 to your computer and use it in GitHub Desktop.
Usage of companion objects
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
package nl.avwie | |
interface Tag<T> | |
interface Tagged<T> { | |
val tag: Tag<T> | |
} | |
class ComponentBag { | |
private val components = mutableMapOf<Tag<*>, Any>() | |
fun <T : Tagged<T>> set(component: T) { | |
components[component.tag] = component | |
} | |
@Suppress("UNCHECKED_CAST") // but we are bloody sure the tag is correct | |
operator fun <T> get(tag: Tag<T>): T? = components[tag] as? T | |
} | |
data class Name(val value: String): Tagged<Name> { | |
override val tag: Tag<Name> = Name // <- this is the companion object | |
companion object : Tag<Name> | |
} | |
data class Age(val value: Int): Tagged<Age> { | |
override val tag: Tag<Age> = Age | |
companion object : Tag<Age> | |
} | |
data class Health(val value: Int): Tagged<Health> { | |
override val tag: Tag<Health> = Health | |
companion object : Tag<Health> | |
} | |
fun main() { | |
val player = ComponentBag() | |
player.set(Name("Beeblebrox")) | |
player.set(Age(42)) | |
player.set(Health(1000)) | |
// typesafe fetch | |
val age = player[Age] // this is automatically if type Age? | |
val health = player[Health] // this is automatically of type Health? | |
require(age?.value == 42) | |
require(health?.value == 1000) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment