Why not support var myArray: [weak MyClass]

I think the reason why [weak MyClass] doesn't exist is because of its behaviour when the reference is deallocated. weak vars need to be optional, because it turns nil when the reference is deallocated. Unowned just assumes that its allocated and crashes if its not.

In [weak MyClass], if one of the references is deallocated, what would happen? Would it remove itself from the array? That might cause some confusion when elements go poof randomly. Would it be like an optional and turn nil? Then the definition [weak MyClass] might not sufficently make it clear that its not [MyClass] but rather [MyClass?].

Of course, there may be ways to achieve a weak array in swift already, via property wrappers or whatnot. Syntax to do it easily in swift, however, might need to go through a few revisions before being added.

1 Like