is there any built in construct to deal with somet...
# getting-started
t
is there any built in construct to deal with something like this?
Copy code
var initialized = false
if (!initialized) {
    ...
    initialized = true
}
l
in what way would you want to deal with it?
t
that i don't know, this just feels very old fashioned
kotlin has a lot of clever ideas, so just wondering if there's something less clunky for cases like these
k
I've had the same feeling in the past, but most of the time I manage to redesign everything to get this to go away. You might be able to use the
lazy
delegate in your case?
t
i don't really see how that can be done (but that might just be me being blind). for context i'm writing to an output stream, and i need to do some special handling on the first write:
Copy code
var initialized = false
override fun write(b: ByteArray, off: Int, len: Int) {
    if (!initialized) {
        ...
        initialized = true
    }
    when {
        ...
    }
}
l
you could to something like this
Copy code
fun initIfNecessary(isIinit: Boolean, initialize: () -> Unit): Boolean {
    if (!isIinit) {
        initialize()
    }
    return true
}
but that doesn't really help anything...
t
😄
no, i realize i can do something like that, was hoping for something built in
i want that branch to be dead for all subsequent calls to write
l
this does seem like something you could use lazy for ;D
Copy code
val toInitialize by lazy {
        /**Initialization*/
        10
    }

    fun doSomething() {
        println(toInitialize)
    }
k
Copy code
class Foo(outputStream: OutputStream) {
    val output by lazy { 
        outputStream.write(5)
        outputStream
    }
    
    fun write(x: Int) {
        output.write(x)
    }
}
t
@karelpeeters i still need to respect the input of the first write call 🤔
l
the first write call influences the initialization, but all other calls shouldnt influence that value?
t
yes
l
i guess lazy doesnt help in that case
k
What kind of weird protocol is that?
t
the first call determines if compression (gzip/brotli) can be enabled
k
Are you sure you can't have the first write value be a constructor parameter instead?
t
yes
here is the full context:
Copy code
override fun write(b: ByteArray, off: Int, len: Int) {
    if (!initialized) { // set available compressors, content encoding, and compressing-stream
        val isCompressible = len >= minSizeForCompression && !excludedMimeType(res.contentType) && res.getHeader(CONTENT_ENCODING).isNullOrEmpty()
        if (isCompressible && rwc.acceptsBrotli && rwc.compStrat.brotli != null) {
            res.setHeader(CONTENT_ENCODING, BR)
            compressingStream = LeveledBrotliStream(res.outputStream, rwc.compStrat.brotli.level)
            brotliEnabled = true
        } else if (isCompressible && rwc.acceptsGzip && rwc.compStrat.gzip != null) {
            res.setHeader(CONTENT_ENCODING, GZIP)
            compressingStream = LeveledGzipStream(res.outputStream, rwc.compStrat.gzip.level)
            gzipEnabled = true
        }
        initialized = true
    }
    when {
        brotliEnabled -> (compressingStream as LeveledBrotliStream).write(b, off, len)
        gzipEnabled -> (compressingStream as LeveledGzipStream).write(b, off, len)
        else -> super.write(b, off, len) // no compression
    }
}
l
Copy code
var foo = 0

val toInitialize by lazy {
    /**Initialization*/
    foo
}

fun doSomething(n: Int) {
    foo = n
    println(toInitialize)
}
t
brb, have to walk my dog ...
k
Yeah no, I can't think of anything better.
t
🤔
thanks for taking a look at least!
k
You could go full state machine but that's not going to make anything more readable.
t
no, i was just wondering if there was some built in construct just for this case
i don't think flipping a bool is horrible, just feels a little dated 🙂
k
True but then again you are implementing compressed streams with ByteArrays 😉
t
😕
i'm aware
can't do much about that though, so trying to make the best of it 😬