Go: proposal: Go2: provide while as alias for for

Created on 22 Jan 2019  ·  4Comments  ·  Source: golang/go

I think, this proposal will get many of :-1: , and it could be issued as normal proposal, but i would like to put it here.

I have learned and written in many languages the last decades; everywhere are discussions (especially for newbies), when to use _for_ and when to use _while_.

With evolution of languages, _for_ stays generally for a counting iteration and _while_ for looping.

When i read my first lines about _Go_ i started wondering, why _Go_ is making so much different, but uses only _for_ - instead of _while_.

Lexically, _for_ is nearly always a _mistake_. Neither _counting_, nor _looping_ nor _ranging_ - lexically more correct is in all cases: _while_ (in other words: _as long as_)

Whereas the one and only case for _for_ is _counting_ and it's ok for _ranging_, a _while_ is lexically correct in _all_ cases:

while {...}
while a < b {...}
while a := 0; a < 10; a++ {...}
while a, b := range c {...}

So, i do not proposal the breaking change of directly renaming for to while, instead i would like to proposal to extend one of the very useful _aliases_ in the language; the first and maybe only one of this kind.

_Go_ is an amazing language and with while it can become more beautiful.

But i cannot define the _costs_ to do this.

Thanks

FrozenDueToAge Go2 LanguageChange Proposal

Most helpful comment

The obvious cost would be that all the programs that use while as names would suddenly break. The langauge would also get a tiny bit more complex. And we'd start style wars over when for or while should be preferred and why.

We already have this issue with the multiple ways to declare variables, for example. Some prefer var s []string, others s := []string{}. I don't think we should add more ways to do the same thing unless there's a clear strong need for it.

All 4 comments

The obvious cost would be that all the programs that use while as names would suddenly break. The langauge would also get a tiny bit more complex. And we'd start style wars over when for or while should be preferred and why.

We already have this issue with the multiple ways to declare variables, for example. Some prefer var s []string, others s := []string{}. I don't think we should add more ways to do the same thing unless there's a clear strong need for it.

First of all, i want to say, i am very new to _Go_, but i like the language very much ❤️

I like it too, that _Go_ is slowly evolving changes and new features 👍

As this is a big deal with the language, i proposed only an alias for for - only because i am troubling with this for longer time and we are talking here about _Go2_, a possibly new decade of _Go_ (whereas _while_ is only some lexical fine-tuning)

About:

We already have this issue with the multiple ways to declare variables, for example.
Some prefer var s []string, others s := []string{}.

I do not feel that's a discus in the same direction; both exists and := is only provided in _function scope_.

The obvious cost would be that all the programs that use while as names would suddenly break.

Refactoring should be absolutely no problem. I have never used while as a name, neither as name, prefix or suffix, so it should be very rarely to talk about.

But i just want to start talking about and getting response; should be said, that all those who are new to _Go_ eventually don't look at _Go2 proposals_.

But, thank you 👍

Lexically, _for_ is nearly always a _mistake_.

If it helps, you can read it as, “for as long as […]”.

Thanks for the suggestion, but this is not a change that we are going to make at this stage in the language's development. There are many things that every new Go programmer has to learn; this is one of the minor ones. Changing this today would break many existing programs, would lead to endless style discussions as @mvdan mentioned, and would benefit very very few people. The cost is too high for the benefit.

Was this page helpful?
0 / 5 - 0 ratings