All Downloads are FREE. Search and download functionalities are using the official Maven repository.

walkmc.Property.kt Maven / Gradle / Ivy

There is a newer version: 2.4.0
Show newest version
package walkmc

import kotlin.properties.*
import kotlin.reflect.*

/**
 * Represents a property utility class.
 */
public data class Property(
	val property: KProperty<*>,
	val oldValue: T,
	val newValue: T,
)


/**
 * Returns a property delegate for a read/write property that calls a specified
 * callback function when changed.
 */
public inline fun  observable(
	initialValue: T,
	crossinline block: Property.() -> Unit,
): ReadWriteProperty =
	Delegates.observable(initialValue) { property, oldValue, newValue ->
		block(Property(property, oldValue, newValue))
	}


/**
 * Returns a property delegate for a read/write property that calls a specified
 * callback function when changed, allowing the callback to veto the modification.
 */
public inline fun  vetoable(
	initialValue: T,
	crossinline block: Property.() -> Boolean,
): ReadWriteProperty =
	Delegates.vetoable(initialValue) { property, oldValue, newValue ->
		block(Property(property, oldValue, newValue))
	}


/**
 * Returns a property delegate for a read/write property with a non-null value
 * that is initialized not during object construction time but at a later time.
 * Trying to read the property before the initial value has been assigned results in an exception.
 */
public fun  notnull() = Delegates.notNull()




© 2015 - 2025 Weber Informatics LLC | Privacy Policy