3 ms·
Go has a great idiom for this, with the 'iota' keyword, which increments by one for each line in a block of declarations. From the language spec: const ( //
by azernik 10y ago
Go has a great idiom for this, with the 'iota' keyword, which increments by one for each line in a block of declarations.
From the language spec:
const ( // iota is reset to 0
a = 1 << iota // a == 1
b = 1 << iota // b == 2
c = 3 // c == 3 (iota is not used but still incremented)
d = 1 << iota // d == 8
)
- bpicolo 10y ago"(iota is not used but still incremented)" Seems bizarre. That's actually a pretty confusing idiom in that context.
- spoiler 10y agoWhile it is a bit odd, it is documented (and I've seen it used a few times in code).
- Ericson2314 10y agoYeah this is classic shitty design from Go. One-off compiler hack that doesn't actually address any of the actual safety problems. org/bitflags/bitflags/macro.bitflags!.html is much better.
- desdiv 10y agohttps://doc.rust-lang.org/bitflags/bitflags/macro.bitflags!.html https://doc.rust-lang.org/bitflags/bitflags/macro.bitflags!.... To save you guys two clicks.
- Ericson2314 10y agoThanks. Phone clipboard failure on my part.