Skip to content

Commit 49b3a44

Browse files
committed
spec mini: Constants/Variables
1 parent b3f8825 commit 49b3a44

File tree

1 file changed

+61
-8
lines changed

1 file changed

+61
-8
lines changed

doc/spec-mini.md

+61-8
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ For backward compatibility, an imaginary literal's integer part consisting entir
117117

118118
### Boolean literals
119119

120-
TODO
120+
The boolean truth values are represented by the predeclared constants `true` and `false`.
121121

122122
```go
123123
true
@@ -216,6 +216,51 @@ nil
216216
iota
217217
```
218218

219+
## Constants
220+
221+
There are _boolean constants_, _rune constants_, _integer constants_, _floating-point constants_, _complex constants_, and _string constants_. Rune, integer, floating-point, and complex constants are collectively called _numeric constants_.
222+
223+
A constant value is represented by a [rune](#rune-literals), [integer](#integer-literals), [floating-point](#floating-point-literals), [imaginary](#imaginary-literals), [boolean](#boolean-literals) or [string](#string-literals) literal, an identifier denoting a constant, a [constant expression](), a [conversion](#conversions) with a result that is a constant, or the result value of some built-in functions such as `min` or `max` applied to constant arguments, `unsafe.Sizeof` applied to [certain values](), `cap` or `len` applied to [some expressions](), `real` and `imag` applied to a complex constant and complex applied to numeric constants. The predeclared identifier `iota` denotes an integer constant.
224+
225+
Not all literals are constants. For example:
226+
227+
```sh
228+
nil
229+
1r
230+
2/3r
231+
```
232+
233+
In general, complex constants are a form of [constant expression]() and are discussed in that section.
234+
235+
Numeric constants represent exact values of arbitrary precision and do not overflow. Consequently, there are no constants denoting the IEEE-754 negative zero, infinity, and not-a-number values.
236+
237+
Constants may be [typed](#types) or _untyped_. Literal constants (including `true`, `false`, `iota`), and certain [constant expressions]() containing only untyped constant operands are untyped.
238+
239+
A constant may be given a type explicitly by a [constant declaration]() or [conversion](#conversions), or implicitly when used in a [variable declaration]() or an [assignment statement]() or as an operand in an [expression](#expressions). It is an error if the constant value cannot be [represented]() as a value of the respective type.
240+
241+
An untyped constant has a _default type_ which is the type to which the constant is implicitly converted in contexts where a typed value is required, for instance, in a [short variable declaration]() such as `i := 0` where there is no explicit type. The default type of an untyped constant is `bool`, `rune`, `int`, `float64`, `complex128`, or `string` respectively, depending on whether it is a boolean, rune, integer, floating-point, complex, or string constant.
242+
243+
244+
## Variables
245+
246+
A variable is a storage location for holding a value. The set of permissible values is determined by the variable's [type](#types).
247+
248+
A [variable declaration]() or, for function parameters and results, the signature of a [function declaration]() or [function literal]() reserves storage for a named variable. Calling the built-in function [new]() or taking the address of a [composite literal]() allocates storage for a variable at run time. Such an anonymous variable is referred to via a (possibly implicit) [pointer indirection](#address-operators).
249+
250+
_Structured_ variables of [array](#array-types), [slice](#slice-types), and [class](#classes) types have elements and fields that may be [addressed](#address-operators) individually. Each such element acts like a variable.
251+
252+
The _static type_ (or just _type_) of a variable is the type given in its declaration, the type provided in the new call or composite literal, or the type of an element of a class variable. Variables of interface type also have a distinct _dynamic type_, which is the (non-interface) type of the value assigned to the variable at run time (unless the value is the predeclared identifier `nil`, which has no type). The dynamic type may vary during execution but values stored in interface variables are always [assignable]() to the static type of the variable.
253+
254+
```go
255+
var x any // x is nil and has static type any
256+
var v *T // v has value nil, static type *T
257+
x = 42 // x has value 42 and dynamic type int
258+
x = v // x has value (*T)(nil) and dynamic type *T
259+
```
260+
261+
A variable's value is retrieved by referring to the variable in an [expression](#expressions); it is the most recent value [assigned]() to the variable. If a variable has not yet been assigned a value, its value is the [zero value]() for its type.
262+
263+
219264
## Types
220265

221266
### Boolean types
@@ -404,6 +449,10 @@ error
404449
any
405450
```
406451

452+
### Classes
453+
454+
TODO (classfile)
455+
407456

408457
## Expressions
409458

@@ -416,6 +465,10 @@ echo "Hello world"
416465
echo("Hello world")
417466
```
418467

468+
#### Builtin functions
469+
470+
TODO
471+
419472
### Operators
420473

421474
Operators combine operands into expressions.
@@ -506,7 +559,7 @@ The equality operators == and != apply to operands of comparable types. The orde
506559
* Complex types are comparable. Two complex values u and v are equal if both real(u) == real(v) and imag(u) == imag(v).
507560
* String types are comparable and ordered. Two string values are compared lexically byte-wise.
508561
* Pointer types are comparable. Two pointer values are equal if they point to the same variable or if both have value `nil`. Pointers to distinct [zero-size]() variables may or may not be equal.
509-
* Interface types that are not type parameters are comparable. Two interface values are equal if they have [identical]() dynamic types and equal dynamic values or if both have value `nil`.
562+
* Interface types are comparable. Two interface values are equal if they have [identical]() dynamic types and equal dynamic values or if both have value `nil`.
510563
* A value x of non-interface type X and a value t of interface type T can be compared if type X is comparable and X [implements]() T. They are equal if t's dynamic type is identical to X and t's dynamic value is equal to x.
511564
* Array types are comparable if their array element types are comparable. Two array values are equal if their corresponding element values are equal. The elements are compared in ascending index order, and comparison stops as soon as two element values differ (or all elements have been compared).
512565

@@ -524,7 +577,7 @@ Logical operators apply to [boolean]() values and yield a result of the same typ
524577
! NOT !p is "not p"
525578
```
526579

527-
### Address operators
580+
#### Address operators
528581

529582
For an operand x of type T, the address operation &x generates a pointer of type *T to x. The operand must be addressable, that is, either a variable, pointer indirection, or slice indexing operation; or a field selector of an addressable struct operand; or an array indexing operation of an addressable array. As an exception to the addressability requirement, x may also be a (possibly parenthesized) [composite literal](). If the evaluation of x would cause a [run-time panic](), then the evaluation of &x does too.
530583

@@ -542,7 +595,7 @@ var x *int = nil
542595
&*x // causes a run-time panic
543596
```
544597

545-
### Conversions
598+
#### Conversions
546599

547600
A _conversion_ changes the [type](#types) of an expression to the type specified by the conversion. A conversion may appear literally in the source, or it may be _implied_ by the context in which an expression appears.
548601

@@ -565,7 +618,7 @@ func() int(x) // x is converted to func() int (unambiguous)
565618

566619
A [constant]() value `x` can be converted to type `T` if `x` is [representable]() by a value of `T`. As a special case, an integer constant `x` can be explicitly converted to a [string type]() using the [same rule]() as for non-constant `x`.
567620

568-
Converting a constant to a type that is not a type parameter yields a typed constant.
621+
Converting a constant to a type yields a typed constant.
569622

570623
```go
571624
uint(iota) // iota value of type uint
@@ -582,7 +635,7 @@ int(1.2) // illegal: 1.2 cannot be represented as an int
582635
string(65.0) // illegal: 65.0 is not an integer constant
583636
```
584637

585-
#### Conversions between numeric types
638+
##### Conversions between numeric types
586639

587640
For the conversion of non-constant numeric values, the following rules apply:
588641

@@ -592,10 +645,10 @@ For the conversion of non-constant numeric values, the following rules apply:
592645

593646
In all non-constant conversions involving floating-point or complex values, if the result type cannot represent the value the conversion succeeds but the result value is implementation-dependent.
594647

595-
#### Conversions to and from a string type
648+
##### Conversions to and from a string type
596649

597650
TODO
598651

599-
#### Conversions from slice to array or array pointer
652+
##### Conversions from slice to array or array pointer
600653

601654
TODO

0 commit comments

Comments
 (0)