martmists
01/05/2024, 11:56 AMthis
to a parent constructor, what should I do? Example:
class LzCompressedReader(private val input: InputStream) : InputStream, MyDataReader(this) {
// Acts as MyDataReader, but pre-processes the InputStream to decompress
}
Joffrey
01/05/2024, 11:59 AMthis
is not fully initialized at that point. Could you please elaborate on why you want to pass this
to the parent constructor?martmists
01/05/2024, 12:00 PMJoffrey
01/05/2024, 12:04 PMLzCompressReader
an InputStream
AND a MyDataReader
? This class hierarchy looks strange.
Also, why does MyDataReader
need this
and not just input
? Maybe you could use inheritance and override the methods of MyDataReader
from your reader if you just want to slightly change the behaviour. Or maybe you could use composition + delegation (especially if MyDataReader
implements an interface).Joffrey
01/05/2024, 12:06 PMclass LzCompressedInputStream(source: InputStream) : InputStream {
// wrap your things there
}
// and then:
val reader = MyDataReader(LzCompressedInputStream(source))
martmists
01/05/2024, 12:06 PMmartmists
01/05/2024, 12:07 PMreader
? Because it needs some data from there for processing (as Lz isn't exactly what's happening, but a more easily understood format)martmists
01/05/2024, 12:07 PMJoffrey
01/05/2024, 12:12 PMMyDataReader
is not properly designed for inheritance and you're trying to use inheritance with it. And/or, LzCompressedReader
mixes low-level responsiblities from an input stream with higher-level responsibilities from a reader.
Maybe the solution would be to extract some functionality from the "single function" of MyDataReader
into multiple re-usable protected
functions that inheritors could reuse to compose them differently. Or use composition instead, but that might also require changes in the design of MyDataReader
.