4 ms·
> 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
by akira2501 2y ago
> 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.
- Brian_K_White 2y agoHow 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?
- akira2501 2y ago> 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.
- plorkyeran 2y agoThe basic use of iota is just what C enums do by default.
- akira2501 2y agoThat'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.
- kelnos 2y ago> 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.
- unsnap_biceps 2y agoDoes B == 'B' in your example?
- plorkyeran 2y agoYes, C enums increment by one for each case if no value is specified.