Ice cream class with error

class IceCream {
    val name: String = "99"
    val ingredients: ArrayList<String> by lazy {
        ingredients.add("flake"); ingredients.add("ice cream scoup")
; ingredients.add("cone")   }
    get() = ingrediants
}

how do I execute mutiple statements in this lambda statement?

It was complaining about it not having a property. I added a property but I’m not sure this is the correct format for it. Please help.

To answer the first question: you can include multiple statements either by putting them on separate lines (like most Kotlin code), or by separating them with semicolons, as you’ve done here (which is just about the only time you need to use them).

However, there are deeper problems here. The lambda can’t use ingredients, because it’s set from the lambda in the first place! (Right now it never even creates the list in the first place.)

And the lambda should return the initialised list; but all it returns now is the result of the last add() call (which is a Boolean).

And the getter is even worse. A getter applies to a property, not to a class — so, despite the indentation, it applies to the ingredients property. This means it can’t be implemented by getting the ingredients property — that would just keep call itself until it ran out of stack space! (Right now it’s misspelled, so won’t even do that… Also, ‘scoop’ is misspelled, but the compiler doesn’t care about that!)

I think the most direct fix for the lambda would be along the lines of:

val ingredients: ArrayList<String> by lazy {
    val i = ArrayList<String>(); i.add("flake"); i.add("ice cream scoop"); i.add("cone"); i
}

(Though that would be better split across lines. IME semicolons are very rarely justified.)

However, Kotlin can do that more concisely:

val ingredients: ArrayList<String> by lazy {
    ArrayList<String>().apply {
        add("flake")
        add("ice cream scoop")
        add("cone")
    }
}

And in this case, there’s a library function which can do it with just:

val ingredients: ArrayList<String> by lazy {
    arrayListOf("flake", "ice cream scoop", "cone")
}

Thanks