The idiomatic way would be to use the `Array` cons...
# getting-started
d
The idiomatic way would be to use the
Array
constructor:
Array(3) { index -> value }
, which initializes the array immediately, so you can have a non-null component type.
👍 1
h
Please allow a follow-up question, sorry. 😅
val squares = Array(8) { Array<Square>(8) { index -> Square(index, index)}}
index
refers to the "inner" index - is there a way to get the "outer" index, so I could do
Square(iOuter, iInner)
?
d
Copy code
val squares = Array(8) { iOuter ->
    Array<Square>(8) { iInner -> {
        // produce square
    }
}
h
nice, thanks
d
These things are just lambdas you are passing to the array constructor, they have normal lambda parameters.
You can even pass a function reference or anything you want, really.
h
still struggling a bit with the syntax, but I''m getting there 😅
d
Array(8, { index -> produceElement() })
is equivalent, but if the last parameter is a lambda, you can pass it outside the paretetheses.
👍 1