https://kotlinlang.org logo
b

blackstardlb

09/21/2020, 1:43 PM
Proposal: keyword to expose a val/var as an interface or a parent class. Having to write private backing properties requires a lot of boilerplate especially if you have many properties to expose.
Copy code
class Test1 {
    private val _subject: Subject<String> = BehaviorSubject.createDefault("Test")
    val subject: Observable<String> = _subject
    fun onEvent(event: String) = _subject.onNext(event)
}

class Test2 {
    val subject: Observable<String> = BehaviorSubject.createDefault("Test")
    fun onEvent(event: String) = (subject as BehaviorSubject<String>).onNext(event)
}

class Proposal {
    exposed:Observable<String> val subject: BehaviorSubject<String> = BehaviorSubject.createDefault("Test")
    protected exposed:Observable<String> val subject2: BehaviorSubject<String> = BehaviorSubject.createDefault("Test")
    fun onEvent(event: String) = subject.onNext(event)
}

fun main() {
    // should all be of type Observable<String>
    val test = Test().subject
    val test2 = Test2().subject
    val proposal = Proposal().subject
}
Test1 shows the private backing property method. Test2 shows using the val as the interface / parent class and casting everywhere with as. Proposal show an example syntax of how this could be made less verbose and more readable.
n

Nir

09/21/2020, 11:47 PM
Wouldn't the classic example for this be a MutableList member with a getter returning List?
b

blackstardlb

09/22/2020, 8:55 AM
@Nir yes