Resources in Arrays and Dictionaries
resource R {}
// Declare a constant for an array of resources.
// Create two resources and move them into the array.
//
let resources <- [
<-create R(),
<-create R()
]
// Invalid: Reading an element from a resource array is not allowed.
//
let firstResource <- resources[0]
// Invalid: Setting an element in a resource array is not allowed,
// as it would result in the loss of the current value.
//
resources[0] <- create R()
// Instead, when attempting to either read an element or update an element
// in a resource array, use a swap statement with a variable to replace
// the accessed element.
//
var res <- create R()
resources[0] <-> res
// `resources[0]` now contains the new resource.
// `res` now contains the old resource.
// Use the shift statement to move the new resource into
// the array at the same time that the old resource is being moved out
let oldRes <- resources[0] <- create R()
// The old object still needs to be handled
destroy oldResLast updated