← Back to context

Comment by amluto

8 hours ago

The array thing is messy. It seems to me that there are potentially four kinds of arrays: the array itself can be copyable or not, and the contents can be copyable or not. But an array of copyable objects can be copied (possibly inefficiently depending what you're doing) whether the creator of the array wants you to or not, and a copyable array of noncopyable objects lets you copy the objects by copying the array. So maybe only two cases are really useful: when the copyability of the array matches the copyability of the objects.

In the everything-copyable case, you can just read an element.

In the nothing-copyable case, the syntax is irrelevant: the operation (arr, elem) = arr.read(index) is invalid.

You may want to take a look at how Rust deals with this. In Rust, even if T: !Copy, you can take a reference to an array element. If a language can't manage this sort of reference, you may need a more restrictive mechanism, perhaps as a pair of swaps (but then you need a default value) or some mechanism using closures that get called on the element and are required to return it.