[Pitch] `With` functions in the standard library

Your concern about introducing a method across all types is valid, but it's worth noting that this precedent has already been set by SE-0161, where keypath subscripts are defined as extensions on Any. I consider this seal already broken.

Indeed, the motivation section of the proposal explicitly highlights the goal of 'avoiding the mutable variable'. The topic has also been subject to prior discussions, where the pitch outlined several other compelling factors, including considerations related to visual grouping.

Additionally, there's precedence in other languages and their standard library functions pursuing similar objectives:

Much like the map operation on Sequence, I believe this proposal is more than mere syntactic sugar. It serves a vital purpose by providing a concise, immutable, and straightforward way to achieve a specific use-case while sidestepping temporary mutable states. There's no intermediate state, all mutations happen in the same "transaction" and the result is immediately encapsulated into an immutable let.

As for the naming concern, I understand your point. While the term with might not be the most intuitive when chained, I find inspiration in Kotlin's apply , and applied or mutated could potentially be Swiftier alternatives.

4 Likes