on 'while'

143 views
Skip to first unread message

Oliver Smith

unread,
Dec 7, 2020, 2:17:23 PM12/7/20
to golang-nuts
Recognizing this is likely to be a dead horse already flogged to infinity, I've been unsuccessful trying to find discussions on the topic of why while was nixed in-favor of the more verbose and error-prone for-based implementations, hoped someone could furnish links?

c.f

// concise, non-repetitive, positively-expressed continuation-clause:
while n := parser.Next(); isAlpha(n) {
  // .. use of n ..
}

vs

// compact but duplicative.
for n := parser.Next(); isAlpha(char); n = parser.Next() {
  // .. use of n ..
}

// otherwise; negated logic to express the 'break' up-front
for {
  n := parser.Next()
  if !isAlpha(n) {
    break
  }
  // .. use of n ..
}
// or additional nesting and "break escape" risk to express positively
for {
  if n = parser.Next(); isAlpha(n) {
  // .. use n, make sure you remember to continue ..
  }
  break
}

//or; negated logic

var n rune
for {
  if n = parser.Next(); !isAlpha(n) {
    break
  }
  // .. use of n ..
}

Ian Lance Taylor

unread,
Dec 7, 2020, 5:27:43 PM12/7/20
to Oliver Smith, golang-nuts
On Mon, Dec 7, 2020 at 11:18 AM Oliver Smith
<oliver...@superevilmegacorp.com> wrote:
>
> Recognizing this is likely to be a dead horse already flogged to infinity, I've been unsuccessful trying to find discussions on the topic of why while was nixed in-favor of the more verbose and error-prone for-based implementations, hoped someone could furnish links?

Using a single looping construct, for, dates back to the earliest days
of Go (well before I joined the project). There are no discussions or
links available. Back then it was just whiteboards and talking.

Go's "for" statement does, of course, support exactly the same
functionality as the "while" statement in C and similar languages (as
well as serving the functionality of the "for" statement). The
construct missing in Go is not "while" but "do/while".


> // concise, non-repetitive, positively-expressed continuation-clause:
> while n := parser.Next(); isAlpha(n) {
> // .. use of n ..
> }

Well, this doesn't work in C or related languages either. You may
want to take a look at https://golang.org/issue/21855.

Ian
Reply all
Reply to author
Forward
0 new messages