Is there a reason why ReadWriteProperty interface ...
# announcements
b
Is there a reason why ReadWriteProperty interface does not extend ReadOnlyProperty?
e
yes it does? https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.properties/-read-write-property/
Copy code
interface ReadWriteProperty<in T, V> : ReadOnlyProperty<T, V>
b
Odd, intellij shows that it doesn't.
image.png
Gradle build is also failing. I'm on kotlin 1.3.72, though as it's a gradle plugin project.
e
b
Thanks!
v
If you target Gradle 6.8, you can use 1.4 🙂
b
Is it out yet? 😮
v
Ah, no, not yet 😄
6.7 was released 12 days ago
But 6.8 will have 1.4 embedded
👍 1
b
How/where I can check supported kotlin version in each gradle version?
v
Well, at least from the JARs in the class path / distribution
e
also you generally don't need to think about the ReadOnlyProperty/ReadWriteProperty interfaces. as the docs say, property delegation works with whatever .getValue()/.setValue() operator functions are in scope
v
Dunno whether there is some table somewhere, I just know for 6.8 1.4 will be embedded, but I think still using 1.3 syntax for the Kotlin DSL, but you don't have conflicts anymore then when using 1.4 stuff in a pre-compiled script plugin and similar things
b
@ephemient I'm aware, however my use-case is taking these delegates as arguments, so I have to use the interfaces.
Basically chained delegates with fallbacks
Copy code
internal class ChainedProperty<R, V>(
  private var main: ReadWriteProperty<R, V?>,
  private val fallback: ReadWriteProperty<R, V>
) : ReadWriteProperty<R, V> {
  override fun getValue(thisRef: R, property: KProperty<*>): V {
    return main.getValue(thisRef, property) ?: fallback.getValue(thisRef, property)
  }

  override fun setValue(thisRef: R, property: KProperty<*>, value: V) {
    main.setValue(thisRef, property, value)
  }
}

internal infix fun <R, V> ReadWriteProperty<R, V?>.or(fallback: ReadWriteProperty<R, V>) = ChainedProperty(this, fallback)
Now I'm forced to duplicate all that for ReadOnlyProperty
Gonna wait for gradle 6.8 I guess. Thanks guys!
v
Unfortunate that there are no denotable union types yet
That would probably also have helped
b
Ah, yes. Although that'd be more of a hack 😄
type MyProp = ReadWriteProperty<R, V?> | ReadOnlyProperty<R, V?>
e
you could somewhat reduce repetition by moving the logic out of the class (although you will have to do a bit of name mangling to keep the JVM happy)