umar
04/18/2017, 4:16 AMvar s : String? = null
exec {
s = createS()
}
s!!.doSomething()
the exec block is executed synchronously so s will be assigned to a non-null value, but the type system doesn’t know this hence the !!. createS() can only be called within the exec block.
Obviously the !! is ugly, I’m trying to figure out whether there is a better way to get s “out” of the inner code block. I could modify exec such that it will return whatever is returned by the code block, but what if I need to get multiple variables out of the code block?
Thread in Slack Conversation
ianclarke: maybe you need to return data class from `exec`:
data class Data(val str: String, val num: Double)
So you can use as normal class:
val data = exec {
Data(str = createS(), num = createD())
}
data.str.doSomething()
data.num.doSomething()
Or destructure it immediately:
val (str, num) = exec {
Data(str = createS(), num = createD())
}
str.doSomething()
num.doSomething()