comparison src/name/blackcap/clipman/Misc.kt @ 50:3f8409470fdf

Input type to SetOnceImpl cannot be nullable.
author David Barts <n5jrn@me.com>
date Sun, 12 Apr 2020 18:45:23 -0700
parents 19d9da731c43
children
comparison
equal deleted inserted replaced
49:bb80148e2cb3 50:3f8409470fdf
24 * needed in Swing, because some vars inevitably need to be declared at 24 * needed in Swing, because some vars inevitably need to be declared at
25 * outer levels but initialized in the Swing event dispatch thread. 25 * outer levels but initialized in the Swing event dispatch thread.
26 * 26 *
27 * @param &lt;T&gt; type of the associated value 27 * @param &lt;T&gt; type of the associated value
28 */ 28 */
29 class SetOnceImpl<T>: ReadWriteProperty<Any?,T> { 29 class SetOnceImpl<T: Any>: ReadWriteProperty<Any?,T> {
30 private var setOnceValue: T? = null 30 private var setOnceValue: T? = null
31 31
32 override operator fun getValue(thisRef: Any?, property: KProperty<*>): T { 32 override operator fun getValue(thisRef: Any?, property: KProperty<*>): T {
33 if (setOnceValue == null) { 33 if (setOnceValue == null) {
34 throw RuntimeException("${property.name} has not been initialized") 34 throw RuntimeException("${property.name} has not been initialized")
48 48
49 /** 49 /**
50 * Normal way to create a setOnce var: 50 * Normal way to create a setOnce var:
51 * var something: SomeType by setOnce() 51 * var something: SomeType by setOnce()
52 */ 52 */
53 fun <T> setOnce(): SetOnceImpl<T> = SetOnceImpl<T>() 53 fun <T: Any> setOnce(): SetOnceImpl<T> = SetOnceImpl<T>()
54 54
55 /** 55 /**
56 * Run something in the Swing thread, asynchronously. 56 * Run something in the Swing thread, asynchronously.
57 * @param block lambda containing code to run 57 * @param block lambda containing code to run
58 */ 58 */