I have a question about local variables. So since ...
# announcements
a
I have a question about local variables. So since you can declare a local variable without an initializer which basically makes them marked with lateinit by default right? And if it doesn't have nullability, and when I try to use it after init'ing it with a conditional, (like the example below), and then I wanna try using it afterwards, then, even tho it's not nullable, it, by default, is null right? And if that's the case, then how can I check if it is init'd even though it's nonnull? Here's an example:
Copy code
fun ArrayList<ItemStack>.tick(){
    var emptyStack: ItemStack
    for(stack in this){
        if(emptyStack == null) {
            if (stack.isEmpty) {
                emptyStack = stack
            }
        }
    }
}
looking at your code it also doesn't compile because
emptyStack
must be initialized
Copy code
fun ArrayList<ItemStack>.tick() {
    val emptyStack = first(ItemStack::isEmpty)
}
👍 1
a
Lol yes that is my point exactly. It isn't initialize but I wanna init it after it's declaration
But compiler won't let me because it's not initialized. But thank you for the link
k
then make it lateinit
a
It's local
k
which is supported since 1.2
a
But I never thought to init it to just a default/dummy value. It's quite late for me and I really just wanna finish this up. Thanks
k
but you should really just avoid using lateinit
a
Yes I know that, a lot of times when I use lateinit I end up just setting it to a default of null