Maybe defaulting to ref
(inferred mutability) would be a good option:
- no copies by default
- allows for mutation if the list is mutable
- no „surprise“ because it is the same as if you access the list entries directly outside the
for
loop
fn test(list: List[Int]):
list[0] = 1 # error
for i in list:
i = 1 # error
fn test(read list: List[Int]):
list[0] = 1 # error
for i in list:
i = 1 # error
fn test(mut list: List[Int]):
list[0] = 1 # ok, mutation inside list
for i in list:
i = 1 # ok, mutation inside list
fn test(owned list: List[Int]):
list[0] = 1 # ok, mutation inside list
for i in list:
i = 1 # ok, mutation inside list
fn test():
var list = [0]
list[0] = 1 # ok, mutation inside list
for i in list:
i = 1 # ok, mutation inside list
fn test(list: List[Int]):
var x = list[0]
x = 1 # ok, mutating copy
for var i in list:
i = 1 # ok, mutating copy