Skip to content

Commit dca7ae7

Browse files
committed
mini spec: Constant declarations
1 parent f168790 commit dca7ae7

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

doc/spec-mini.md

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1808,7 +1808,43 @@ Given a set of identifiers, an identifier is called _unique_ if it is _different
18081808

18091809
### Constant declarations
18101810

1811-
TODO
1811+
A constant declaration binds a list of identifiers (the names of the constants) to the values of a list of [constant expressions](#constant-expressions). The number of identifiers must be equal to the number of expressions, and the nth identifier on the left is bound to the value of the nth expression on the right.
1812+
1813+
```go
1814+
ConstDecl = "const" ( ConstSpec | "(" { ConstSpec ";" } ")" ) .
1815+
ConstSpec = IdentifierList [ [ Type ] "=" ExpressionList ] .
1816+
1817+
IdentifierList = identifier { "," identifier } .
1818+
ExpressionList = Expression { "," Expression } .
1819+
```
1820+
1821+
If the type is present, all constants take the type specified, and the expressions must be [assignable]() to that type, which must not be a type parameter. If the type is omitted, the constants take the individual types of the corresponding expressions. If the expression values are untyped [constants](#constants), the declared constants remain untyped and the constant identifiers denote the constant values. For instance, if the expression is a floating-point literal, the constant identifier denotes a floating-point constant, even if the literal's fractional part is zero.
1822+
1823+
```go
1824+
const Pi float64 = 3.14159265358979323846
1825+
const zero = 0.0 // untyped floating-point constant
1826+
const (
1827+
size int64 = 1024
1828+
eof = -1 // untyped integer constant
1829+
)
1830+
const a, b, c = 3, 4, "foo" // a = 3, b = 4, c = "foo", untyped integer and string constants
1831+
const u, v float32 = 0, 3 // u = 0.0, v = 3.0
1832+
```
1833+
1834+
Within a parenthesized const declaration list the expression list may be omitted from any but the first ConstSpec. Such an empty list is equivalent to the textual substitution of the first preceding non-empty expression list and its type if any. Omitting the list of expressions is therefore equivalent to repeating the previous list. The number of identifiers must be equal to the number of expressions in the previous list. Together with the [iota constant generator](#iota) this mechanism permits light-weight declaration of sequential values:
1835+
1836+
```go
1837+
const (
1838+
Sunday = iota
1839+
Monday
1840+
Tuesday
1841+
Wednesday
1842+
Thursday
1843+
Friday
1844+
Partyday
1845+
numberOfDays // this constant is not exported
1846+
)
1847+
```
18121848

18131849
### Iota
18141850

0 commit comments

Comments
 (0)