News for hackers. Stuff that matters.
Re:The Smallest Thing in Go by Brian_K_White on Sunday December 08, 2024 @11:34PM (#42363091)
This looks like plain old enum in plain c.

Did I miss something that makes it remarkable?

Re:The Smallest Thing in Go by iterance on Sunday December 08, 2024 @11:59PM (#42363186)
The answer is supposed to be "iota," but imo iota is not really remarkable by itself. I mean it's nice, it's not pointless (though arguably it isn't parsimonious), but you could just type in the numbers. It's not saving you that much.

What I'd really want is something that clearly differentiates enums where the values do matter and the values don't matter, which iota comes so close to being, but then you can do math with it. To use their example, it doesn't really matter if "electron" is 4 or 8 or whatever, but I'm sure as hell not going to assign 5 to January.

Re:The Smallest Thing in Go by akira2501 on Sunday December 08, 2024 @11:36PM (#42363103)
> This looks like plain old enum in plain c.

It very nearly is.

> Did I miss something that makes it remarkable?

Yes. The literal 'iota' keyword. There is no equivalent to this in C.

Re:The Smallest Thing in Go by Brian_K_White on Monday December 09, 2024 @12:20AM (#42363252)
How is that remarkable?

Does it have some other actual function and this article is only saying how btw it can also be used to make ersatz enums?

Re:The Smallest Thing in Go by akira2501 on Monday December 09, 2024 @01:00AM (#42363408)
> make ersatz enums?

The author is describing 'iota'. You can easily combine this technique with a custom type wrapper which provides the "full enum" if that's actually what you need. In which case this provides a level of capability and convenience above that of C.

In practice, it really is quite remarkable, when you first use it to it's fullest extent.

Re:The Smallest Thing in Go by plorkyeran on Monday December 09, 2024 @12:41AM (#42363323)
The basic use of iota is just what C enums do by default.
Re:The Smallest Thing in Go by akira2501 on Monday December 09, 2024 @12:52AM (#42363372)
That's one of potentially unlimited use cases. iota composes with just about any static math expression to generate almost any kind of sequence. The most common is obviously conveniently constructing bit vectors; which is possible in C, but not as an automatically composed sequence. This really is a nice feature in practice and certainly not the only type of convenient sequence you can create.

A contrived example might be:

    const (
        A rune = 'A' + iota
        B
    )

    func main() {
        fmt.Printf("Hello %c", B)
    }
You can, of course, just write it like a C enum if you were so inclined and that willing to go out of your way to show your disinterest in something.
Re:The Smallest Thing in Go by kelnos on Monday December 09, 2024 @01:47AM (#42363597)
> You can, of course, just write it like a C enum if you were so inclined and that willing to go out of your way to show your disinterest in something.

Oh c'mon, that's a cheap shot. Your example is odd, as it doesn't really show off anything interesting; I can indeed write it as a C enum, and I can do it with less typing:

    enum {
        A = 'A',
        B,
    };
iota doesn't really do anything interesting there. Much more interesting, IMO, was the example in the article where it can be used to easily construct bitfields. That shows off the "just about any static math expression" that you point out.

I think my problem with it overall is that it's weird and unexpected. Even when it can be genuinely useful, it doesn't really fit in the language as a part of syntax. Then again, I feel like that's pretty common for Go.

Re:The Smallest Thing in Go by unsnap_biceps on Tuesday December 10, 2024 @03:41AM (#42375017)
Does B == 'B' in your example?
Re:The Smallest Thing in Go by plorkyeran on Tuesday December 10, 2024 @11:35PM (#42384826)
Yes, C enums increment by one for each case if no value is specified.
Re:The Smallest Thing in Go by Someone on Monday December 09, 2024 @01:27AM (#42363511)
The thing that’s remarkable is that, with iota, you can specify an expression that computes the enum value:

For example, this:

  const (
    Foo = 2 * iota // 0
    Bar = 2 * iota // 2
    Baz = 3 * iota // 6
    // ...
  )
It typically is combined with another feature, explicit repetition of the last non-empty expression list (https://go.dev/ref/spec#Constant_declarations) to avoid having to repeat the iota expression.

For a powerful example see https://go.dev/ref/spec#Iota:

By definition, multiple uses of iota in the same ConstSpec all have the same value:

  const (
    bit0, mask0 = 1 << iota, 1<<iota - 1  // bit0 == 1, mask0 == 0  (iota == 0)
    bit1, mask1                           // bit1 == 2, mask1 == 1  (iota == 1)
    _, _                                  // (iota == 2, unused)
    bit3, mask3                           // bit3 == 8, mask3 == 7  (iota == 3)
  )
I think that’s bordering on “too smart for it’s own good”, but it does prevent a very common copy-paste bug in C, where one would write something like

  enum {
    bit0 = 1 << 0,
    bit1 = 1 << 1,
    bit3 = 1 << 3,

    mask0 = bit0 - 1
    mask1 = bit1 - 1,
    mask3 = bit1 - 1 // copy-paste error
  }
(Happens fairly easily in real world code when the Enums are long, and extra Enums get added over time)
Re:The Smallest Thing in Go by Brian_K_White on Monday December 09, 2024 @12:45PM (#42368398)
Thank you. Arbitrary expression is what I was missing.
Re:The Smallest Thing in Go by pjmlp on Monday December 09, 2024 @03:03AM (#42363955)
It is Go, apparently having plain old enums is too advanced.
Re:The Smallest Thing in Go by sir_eliah on Monday December 09, 2024 @05:01AM (#42364616)
It's one of my major pain points with Go. When can we have proper enum support with compile-time checks whether all the enum values are exhausted?
Re:The Smallest Thing in Go by kubb on Monday December 09, 2024 @07:08AM (#42365446)
In about 10 years, maybe :)