Skip to content

Commit

Permalink
Merge pull request #2018 from xushiwei/q
Browse files Browse the repository at this point in the history
mini spec: Declarations and scope
  • Loading branch information
xushiwei authored Nov 20, 2024
2 parents 06a9e20 + ec0c65b commit 743944a
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion doc/spec-mini.md
Original file line number Diff line number Diff line change
Expand Up @@ -1737,7 +1737,37 @@ println
...
```

## Functions
## Declarations and scope

A _declaration_ binds a non-[blank]() identifier to a [constant](), [type](), [variable](), [function](), [label](), or [package](). Every identifier in a program must be declared. No identifier may be declared twice in the same block, and no identifier may be declared in both the file and package block.

The [blank identifier]() may be used like any other identifier in a declaration, but it does not introduce a binding and thus is not declared. In the package block, the identifier `init` may only be used for [init function]() declarations, and like the blank identifier it does not introduce a new binding.

```go
Declaration = ConstDecl | TypeDecl | VarDecl .
TopLevelDecl = Declaration | FunctionDecl .
```

The scope of a declared identifier is the extent of source text in which the identifier denotes the specified constant, type, variable, function, label, or package.

Go+ is lexically scoped using blocks:

* The scope of a [predeclared identifier]() is the universe block.
* The scope of an identifier denoting a constant, type, variable, or function declared at top level (outside any function) is the package block.
* The scope of the package name of an imported package is the file block of the file containing the import declaration.
* The scope of an identifier denoting function parameter, or result variable is the function body.
* The scope of a constant or variable identifier declared inside a function begins at the end of the `ConstSpec` or `VarSpec` (`ShortVarDecl` for short variable declarations) and ends at the end of the innermost containing block.

An identifier declared in a block may be redeclared in an inner block. While the identifier of the inner declaration is in scope, it denotes the entity declared by the inner declaration.

The [package clause]() is not a declaration; the package name does not appear in any scope. Its purpose is to identify the files belonging to the same [package](#packages) and to specify the default package name for import declarations.

### Label scopes

TODO


### Function declarations

A function declaration binds an identifier, the function name, to a function.

Expand All @@ -1760,6 +1790,7 @@ func IndexRune(s string, r rune) int {
}
```


## Packages

Go+ programs are constructed by linking together packages. A package in turn is constructed from one or more source files that together declare constants, types, variables and functions belonging to the package and which are accessible in all files of the same package. Those elements may be [exported]() and used in another package.
Expand Down

0 comments on commit 743944a

Please sign in to comment.