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

Idea: Custom Property Delegate: SetOnlyProperty #72

Closed
gmilette opened this issue Jul 18, 2018 · 1 comment
Closed

Idea: Custom Property Delegate: SetOnlyProperty #72

gmilette opened this issue Jul 18, 2018 · 1 comment

Comments

@gmilette
Copy link

gmilette commented Jul 18, 2018

Currently the code has some verbose syntax for the set only properties like this:

public var disabledIconColor: Long
        @Deprecated(level = DeprecationLevel.ERROR, message = "Non readable property.")
        get() = nonReadable()

        set(value) {
            item.withDisabledIconColor(value.toInt())
        }

We could replace it with syntax like this:

public var disabledIconColorProp by SetOnlyProperty<Int>({ color:Int -> item.withDisabledIconColor(color)})

Using the custom property delegate below.
The only drawback is that the code cannot use the Depreciated syntax. I think that is fine because we have an exception that will tell any programmers not to use the property.

import co.zsmb.materialdrawerkt.NonReadablePropertyException
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty
class SetOnlyProperty<T>(setFunc: (T) -> Unit) : ReadWriteProperty<Any?, T> {
    var setFunc: (T) -> Unit = {}

    override fun getValue(thisRef: Any?, property: KProperty<*>): T {
        nonReadable();
    }

    override fun setValue(thisRef: Any?, property: KProperty<*>, value: T) {
        setFunc(value)
    }

    fun nonReadable(): Nothing = throw NonReadablePropertyException()
}```
@zsmb13
Copy link
Owner

zsmb13 commented Jul 18, 2018

Thanks for bringing this up, Todd Ginsberg suggested the same just a week ago on Twitter.

I maintain my thoughts I had there:

  • the extra object allocations for the delegates might be fine (should be tested, might be solved by wrapping them in lazy)
  • getting editing time errors is much preferred to runtime errors in my opinion

@zsmb13 zsmb13 closed this as completed Aug 2, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants