Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

inline class PropertyOrValue<T> #51

Open
Miha-x64 opened this issue Jun 8, 2019 · 0 comments
Open

inline class PropertyOrValue<T> #51

Miha-x64 opened this issue Jun 8, 2019 · 0 comments

Comments

@Miha-x64
Copy link
Owner

Miha-x64 commented Jun 8, 2019

A zero-cost wrapper which acts like Property<T> | T.

// pseudokode, won't kompile
inline class PropertyOrValue<T> {
    private val _value: Any? // = Property<T> | T
    constructor(value: T) {
        this._value = // guard against the (rare) case when value is also a property:
            if (value is Property<*>) immutablePropertyOf(value)
            else value
    }
    constructor(property: Property<T>) {
        this._value = property
    }
    val value: T
        get() = (if (_value is Property<*>) _value.value else _value) as T
    // TODO: addChangeListener(On), removeChangeListener
}

Use-cases:

  • universal [library] components which accept some Properties may accept PropertyOfValue instead, allowing caller to avoid wrapping value into ImmutableProperty
  • Android resources: (typedValue.changingConfigurations & activityInfo.configChanges) == 0 implies Value, else Property (not sure how popular this case is, taking into account that resources are good at storing strings, but other resource types — animations and animators, drawables, colours and colour state lists, layouts, menus, styles, fonts, scalars and scalar arrays — are mostly useless)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant