MaterialDrawerKt
MaterialDrawerKt copied to clipboard
Idea: Custom Property Delegate: SetOnlyProperty
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()
}```
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