Class delegate using internal property


#1

The following code has a compilation error: Unresolved reference internalList

class MyIterable() : Iterable<String> by internalList {
    private val internalList : List<String> = listOf("...")
}

In my use case I need something like that (an RX Observable delegated to internal managed Subject) but I don’t want expose internal state as constructor parameter.

How can I do?
Thanks,
Francesco


Destructuring multiple components
#2

I don’t think it’s possible in Kotlin now.

Related requests:


Unneeded limitations of delegated interfaces
#3

You can make primary constructor private:

class MyIterable private constructor(internalList: List<String>):
    Iterable<String> by internalList {
  constructor() : this(internalList = listOf("")) {}
}

#4

thanks a lot,
but it is a ugly verbose workaround…