PropertyDelegate

class PropertyDelegate<V, O : Property<V>>(initialValue: V, propertyFactory: (Any, String, V) -> O)

Creates a SimpleProperty, where the bean and beanName are automatically set.

V is the value type (Double in the example above).

O is the property type (DoubleProperty) in the example above.

Example usage :

val widthProperty by doubleProperty(1.0)

Property is itself a delegate, which returns the property value. So...

val widthProperty by doubleProperty(1.0)
var width by widthProperty

NOTE. The value of beanName is assumed to be the name of the Property, without the Property suffix. In the example above by doubleProperty(1.0) sees the name widthProperty, and reduces it to just width. This is a problem when you have code such as :

private val privateWidthProperty by doubleProperty(1.0)
val widthProperty = privateWidthProperty.asReadOnly()
var width by privateWidthProperty

In this case, the bean name would be privateWidth, which is wrong. So there are 3 common name prefixes that are tested for :

  1. _ (underscore)

  2. private

  3. mutable

A warning message is issued to Glok's log if the bean is not found.

For any other naming conventions, you'll have to set the bean name manually i.e.

val funnyWidthProperty = SimpleDoubleProperty(1.0, this, "width")
var width by widthProperty

A common mistake is to use PropertyDelegate with non-public fields. Beans must be public. So this is wrong :

private val widthProperty by doubleProperty(1.0)
private var width by widthProperty

Use this instead (without using a PropertyDelegate, and therefore bean and beanName are not set) :

private val widthProperty = SimpleDoubleProperty(1.0)
private var width by widthProperty

Bean name will be blank.

Constructors

Link copied to clipboard
constructor(initialValue: V, propertyFactory: (Any, String, V) -> O)

Functions

Link copied to clipboard
operator fun getValue(thisRef: Any, kProperty: KProperty<*>): O