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
object MyFirstSingleton { | |
var mutablePropertyFTW = 5 | |
} |
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
object MyFirstGenericSingleton<T> { | |
var mutablePrpertyFTW: T | |
} |
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
interface SomeThing { | |
fun run() | |
fun play() | |
} |
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 Thing1: SomeThing { | |
override fun run() = println("Run Fast!") | |
override fun play() = println("Play Hard!") | |
} | |
class Thing2: SomeThing { | |
override fun run() = println("Run Silly!") | |
override fun play() = println("Play Soft!") | |
} |
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 ThingFactory { | |
inline fun <reified T: Any> run() { | |
if (myThing == null) { | |
createThing<T>() | |
} | |
myThing!!.run() | |
} | |
inline fun <reified T: Any> play() { | |
if (myThing == null) { |
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
val factory = ThingFactory() | |
factory.run<Thing1>() // "Run Fast!" | |
factory.play<Thing1>() // "Play Hard!" | |
factory.play<Thing2>() // "Play Hard!" ???? |
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 ThingFactory { | |
inline fun <reified T: Any> run() { | |
if (myThing == null) { | |
createThing<T>() | |
} | |
myThing!!.run() | |
} | |
inline fun <reified T: Any> play() { | |
if (myThing == null) { |
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
val factory = ThingFactory() | |
factory.run<Thing1>() // "Run Fast!" | |
factory.play<Thing1>() // "Play Hard!" | |
factory.play<Thing2>() // "Play Hard!" ???? | |
interface SomeThing { | |
fun run() | |
fun play() | |
} |
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
import kotlin.reflect.KClass | |
import kotlinx.coroutines.* | |
fun main() { | |
println("Welcome to Thingville, population 1") | |
val factory = ThingFactory(Thing1::class) | |
factory?.run() // "Run Fast!" | |
factory?.play() // "Run Hard!" | |
val factory2 = ThingFactory(Thing2::class) // Run Time Error: Already created as type: class Thing1 | |
val factory3 = ThingFactory(NoThing::class) // Compile Time Error: None of the following functions can be called with the arguments supplied: public constructor ThingFactory(clazz: ThingFactory.ThingType) defined in ThingFactory public inline fun <reified T : SomeThing> ThingFactory(clazz: KClass<???>): ThingFactory? |
OlderNewer