From the Terraform docs:
element(list, index) - Returns a single element from a list at the given index. If the index is greater than the number of elements, this function will wrap using a standard mod algorithm.
What would be a good reason to wrap using mod? This behavior seems to me like it could be the cause of lots of headaches.
At the top of my head I can only remember two other approaches to handle accessing an element that's out of bounds:
- Python/Ruby: return None/Nil
- Java/JS/Ruby: Raise an error
I'm so used to them that they seem to make sense, you either get nothing or an error but why would you ever expect to get the k mod n
element in the list? If you were the implementer, how would you justify this choice of behavior.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…