Skip to content

Commit

Permalink
mini spec: Iota
Browse files Browse the repository at this point in the history
  • Loading branch information
xushiwei committed Dec 2, 2024
1 parent dcd781a commit d268979
Showing 1 changed file with 38 additions and 1 deletion.
39 changes: 38 additions & 1 deletion doc/spec-mini.md
Original file line number Diff line number Diff line change
Expand Up @@ -1848,7 +1848,44 @@ const (

### Iota

TODO
Within a [constant declaration](#constant-declarations), the predeclared identifier iota represents successive untyped integer [constants](#constants). Its value is the index of the respective [ConstSpec]() in that constant declaration, starting at zero. It can be used to construct a set of related constants:

```go
const (
c0 = iota // c0 == 0
c1 = iota // c1 == 1
c2 = iota // c2 == 2
)

const (
a = 1 << iota // a == 1 (iota == 0)
b = 1 << iota // b == 2 (iota == 1)
c = 3 // c == 3 (iota == 2, unused)
d = 1 << iota // d == 8 (iota == 3)
)

const (
u = iota * 42 // u == 0 (untyped integer constant)
v float64 = iota * 42 // v == 42.0 (float64 constant)
w = iota * 42 // w == 84 (untyped integer constant)
)

const x = iota // x == 0
const y = iota // y == 0
```

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

```go
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)
)
```

This last example exploits the [implicit repetition](#constant-declarations) of the last non-empty expression list.

### Type declarations

Expand Down

0 comments on commit d268979

Please sign in to comment.