Skip to content

Instantly share code, notes, and snippets.

@markmaynard
markmaynard / cookie-expiry.ts
Created May 30, 2019 15:59
[On behalf of Lexmark International, Inc] Cookie expiry implementation for Idle Timeout
@markmaynard
markmaynard / MyFirstSingleton.kt
Last active June 1, 2019 16:19
My First Kotlin Singleton
object MyFirstSingleton {
var mutablePropertyFTW = 5
}
@markmaynard
markmaynard / MyFirstGenericSingleton.kt
Created June 1, 2019 16:28
Attempt 1 to create generic singleton
object MyFirstGenericSingleton<T> {
var mutablePrpertyFTW: T
}
@markmaynard
markmaynard / SomeThing.kt
Last active June 1, 2019 17:25
Thing interface
interface SomeThing {
fun run()
fun play()
}
@markmaynard
markmaynard / Things.kt
Last active June 7, 2019 16:47
Create some things!
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!")
}
@markmaynard
markmaynard / ThingFactory.kt
Last active June 1, 2019 17:49
ThingFactory attempt number 1
class ThingFactory {
inline fun <reified T: Any> run() {
if (myThing == null) {
createThing<T>()
}
myThing!!.run()
}
inline fun <reified T: Any> play() {
if (myThing == null) {
@markmaynard
markmaynard / thingfactoryuse1.kt
Last active June 8, 2019 12:20
Using my first thing factor
val factory = ThingFactory()
factory.run<Thing1>() // "Run Fast!"
factory.play<Thing1>() // "Play Hard!"
factory.play<Thing2>() // "Play Hard!" ????
class ThingFactory {
inline fun <reified T: Any> run() {
if (myThing == null) {
createThing<T>()
}
myThing!!.run()
}
inline fun <reified T: Any> play() {
if (myThing == null) {
@markmaynard
markmaynard / gsfpt1c.kt
Last active June 10, 2019 11:45
Generic Singleton Factory part 1 complete
val factory = ThingFactory()
factory.run<Thing1>() // "Run Fast!"
factory.play<Thing1>() // "Play Hard!"
factory.play<Thing2>() // "Play Hard!" ????
interface SomeThing {
fun run()
fun play()
}
@markmaynard
markmaynard / gensingletonfactory2.kt
Last active June 10, 2019 12:08
Generic Singleton Factory attempt 2
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?