This file contains hidden or 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
/** | |
* Can assign only once | |
*/ | |
class AssignOnce<T> { | |
private var _backing: T? = null | |
operator fun getValue(thisRef: Any, property: KProperty<*>) = _backing!! | |
operator fun setValue(thisRef: Any, property: KProperty<*>, value: T) { | |
synchronized(this) { |
This file contains hidden or 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
/** | |
* Kotlin try-with-resources practice | |
* All streams want to be auto-closed should run `autoClose()` | |
* Simple as origin syntax! | |
*/ | |
inline fun using(crossinline block: ResourceManager.() -> Unit): Catcher { | |
val manager = ResourceManager() | |
try { | |
// Use manager's auto close | |
manager.use(block) |
This file contains hidden or 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
/** | |
* Support implicit conversion form Any? to boolean | |
* Use JavaScript standard | |
* Example: (_if_({ null }) { 0 } _else_ { 1 })() -> 1 | |
*/ | |
open class _if_<T>(private val condition: () -> Any?, private val ifBlock: () -> T) { | |
companion object { | |
fun Any?.logic(): Boolean { | |
return when (this) { |
This file contains hidden or 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
/** | |
* Kotlin Promise | |
* Just like the Promise in JavaScript | |
* Aplha Standard | |
*/ | |
typealias Resolve<T> = (T) -> Unit // T : Any? | |
typealias OnResolved<T, R> = (T) -> R // T, R : Any? | |
typealias Reject<U> = (U) -> Unit // U : Throwable | |
typealias OnRejected<U, V> = (U) -> V // U, V : Throwable |