Creating lenses with Swift macros (usecase for arbitrary names at the global scope)

I think the more idiomatic approach is @ExFalsoQuodlibet's modifyEach operator above, which was pitched many years ago by the author of the Functional Swift book: In-place map for MutableCollection

Your above example would be written in even fewer characters than the lens approach:

users.modifyEach { $0.name = "new name" }

And using the key path subscript traversal mentioned above, you could technically even do something like:

users[forEach: \.name] = "new name"
6 Likes