[swift-evolution] ed/ing, InPlace, Set/SetAlgebra naming resolution
rainer at brockerhoff.net
Fri Feb 12 07:27:33 CST 2016
On 2/12/16 05:26, Greg Parker via swift-evolution wrote:
> Now y'all are approaching the syntax of a proposal I made long ago for a .= operator. Here's what I wrote then (with a handful of syntax updates).
> 3. Add operator .= for in-place modification.
> Operator .= is analogous to arithmetic operators like += . It is shorthand for `expr = expr.stuff` for the case where `expr` is inconveniently long. (It also doesn't evaluate `expr` twice, if Swift's optimization rules otherwise would allow it or require it for the longhand case.)
> s .= upper()
> // like s = s.upper()
> p .= next
> // like p = p.next
> some().long().expression .= upper().trim().truncateToLength(5)
> // like some().long().expression = some().long().expression.upper().trim().truncateToLength(5)
> As seen in this last example, one advantage of this syntax is that it allows chained mutations but cleanly separates the "lookup" calls from the "mutate" calls. This is an improvement upon language and API designs with mutating methods where the property being changed is buried in the middle of the expression somewhere.
> some().long().expression.upperInPlace().trimInPlace().truncateInPlace(toLength: 5)
> some().long().expression().upper.trimInPlace().truncateInPlace(toLength: 5)
> some().long().expression .= upper().trim().truncate(toLength: 5)
> some().long().expression().upper .= trim().truncate(toLength: 5)
> One consequence of these changes is that compiler optimizations to take advantage of in-place implementation are much more important for performance, because there isn't any syntax to call the in-place implementation directly.
+1 for the .= operator!
In general, I'm in favor of limiting the use of subtleties of English to
denote semantics in programming languages; for non-native English
speakers it's much easier to memorize operators and sigils instead of
having to remember what, say, a past participle is.
FWIW English is my 4th language, but I successfully avoided learning
grammar details in all of them :-). Then again, this rendered me
incapable of learning English-like programming languages such as
Hypertalk, AppleScript and <shudder>COBOL...
Rainer Brockerhoff <rainer at brockerhoff.net>
Belo Horizonte, Brazil
"In the affairs of others even fools are wise
In their own business even sages err."
More information about the swift-evolution