Oftentimes one wishes to assign to a variable a modified version of itself, but a special assignment operator such as += does not exist for that particular modification. Unfortunately, for very long variables, this requires writing out the full name on both sides of the equals sign — for instance, to negate a Bool, some.reallyLong.bool.variable = !some.reallyLong.bool.variable. This is unnecessarily verbose and impairs readability.
I propose the introduction of the keyword itself to refer to the LHS of an assignment on the RHS. This would allow e.g., some.reallyLong.boolean = !itself and some.reallyLong.int = itself * itself - 1.
... or this piece of complicated logic can be extracted into a function that takes LHS inout. As a nice side-effect, you'd get a meaningful name for the operation.
func doubleAndIncrement(_ x: inout Int) {
x = x * x + 1
}
Yes, with seems more appropriate for modifying instance members directly while itself would be used for applying operations or methods returning the same type (like sorted). Certainly possible with with, but less clear. In fact I think with would be better off being non-inout.
That was indeed the inspiration for this idea but being able to refer to the LHS on the RHS in any expression is useful in many other contexts, and is much cleaner than writing a one-off function. Some examples:
Not only are these easier to write, they're (significantly!) easier to read because you don't need to mentally parse the long variable name twice. For comparison:
As @Justin_Jia said, defining a function may be a better fit.
There are some pitches going around for a with function that addresses some of the same issues, but I see that @DevAndArtist already pointed this out. You can already do some of that yourself:
In general, new syntactic sugar is of the lowest priority in Swift's evolution; unless there is something truly astounding made possible, pitching a new keyword seems unlikely to meet with much success.
Just a warning. I don‘t remember if the pure function is bugged but a custom inout operator definetelly is bugged in Swift 4.1. I‘ll update this post when I‘ll be back at my workstation with a link to a bug-issue. There is a compiler bug related to inout which appears on a release build because of some aggressive optimizations and basically will crash your whole application.