4 ms·
> "For" loops may now range over integers The future is now, old man!
by shakow 3y ago
> "For" loops may now range over integers
The future is now, old man!
- maximus-decimus 3y agoWhile the phrasing is funny, it's really more "we now have a direct equivalent to 10.times in ruby"
- mhd 3y agoAlgol 68 had a loop where pretty much any part was optional, so both "TO 10" and "FOR i TO 10" (if you needed a loop variable) were possible. (Back when Go came out, there were some Algol 68 comparisons, IIRC)
- deleted 3y ago[deleted]
- kristianp 3y agofor i := range 10 { What was the syntax for this before?
- tptacek 3y agofor i := 0; i < 10; i++
- kristianp 3y agoThanks. The new syntax emulates the python `for x in range(10):`.
- adtac 3y agoI must be getting old because I almost wish they didn't add this as it's slightly ambiguous whether it's ranging from [0, 10) or [1, 10] and anything ambiguous is probably going to haunt me at 3am some day
- cwbriscoe 3y agoI always just assume it is consistent to whether arrays start at [0] or [1] on whichever language I am working on.
- everybodyknows 3y agoInterestingly, this does not raise any error, rather has no effect. for i := range -10 { panic(i) }
- ctz 3y ago"yes, please run this loop minus 10 times" - statements dreamed up by the utterly Deranged
- maximus-decimus 3y agoBack in my days, you had to do it this way for i := 0; i <=-10; i++{ panic }
- lpribis 3y agoBack in my day you would do for (size_t i = 0; i < -10; i++) { panic } And it would panic 18446744073709551606 times.
- tptacek 3y agoSure, presumably for the same reason a C-style for loop wouldn't do anything weird there either.
- shakow 3y ago> anything ambiguous is probably going to haunt me at 3am some day Man, I hope you never wondered how ERRNO behaves with CGo or that you didn't miss to update any initialization of that struct you recently added a new field to, that haunts me at 3AM.
- konart 3y ago
- metaltyphoon 3y agoWish you could do for i := range [3:10] {
- dgunay 3y agoI think you could write something using the experimental Rangefunc feature that does that.
- icholy 3y agoYou can, which makes me wonder why they added the range-over-int functionality when it could have been a function in the proposed iter package: for x := range iter.N(10) { ... }
- dgunay 3y agoBetter yet, you can name it something like `iter.ZeroTo(10)` and it immediately clears up questions about whether it begins from 0 or 1.
- maximus-decimus 3y agoMy guess because it really doesn't save that many characters compared to for i:= 3 ; i<10; i++ {} I didn't count but it might actually be longer. range 10 is at least shorter
- gocsjess 3y agofor i := range [10]struct{}{} {
- gwd 3y agoI'm not sure what the syntax is now; the release notes say "see the spec for details[link]", but in the link I don't see anything in there about using integers as a range expression.