mirror of
https://github.com/golang/go
synced 2024-11-22 07:14:40 -07:00
Introduced forward declaration of types per discussion with ken.
A forward-declared (or not yet fully defined) type must be used to construct (mutually) recursive type declarations. This simplifies the compiler and resolves the issue at which scope the forward- declared type is to be declared. SVN=117770
This commit is contained in:
parent
f5c0763432
commit
d222c65477
@ -1,6 +1,6 @@
|
|||||||
The Go Programming Language
|
The Go Programming Language
|
||||||
----
|
----
|
||||||
(April 29, 2008)
|
(May 5, 2008)
|
||||||
|
|
||||||
This document is an informal specification/proposal for a new systems programming
|
This document is an informal specification/proposal for a new systems programming
|
||||||
language.
|
language.
|
||||||
@ -678,19 +678,26 @@ Pointer types are similar to those in C.
|
|||||||
|
|
||||||
PointerType = "*" Type.
|
PointerType = "*" Type.
|
||||||
|
|
||||||
We do not allow pointer arithmetic of any kind.
|
Pointer arithmetic of any kind is not permitted.
|
||||||
|
|
||||||
*int
|
*int
|
||||||
*map[string] *chan
|
*map[string] *chan
|
||||||
|
|
||||||
It is legal to write a pointer type (only) such as *T or **T even if T
|
It is legal to write a pointer type (only) such as *T even if T is
|
||||||
is not yet defined as a type name. This allows the construction of
|
an incomplete type (i.e., either not yet fully defined or forward
|
||||||
mutually recursive data types such as structs:
|
declared). This allows the construction of recursive types such as:
|
||||||
|
|
||||||
type S1 struct { s2 *S2 } // S2 is not yet declared
|
type S struct { s *S }
|
||||||
type S2 struct { s1 *S1 }
|
|
||||||
|
|
||||||
By the end of the package source, such types must be fully declared.
|
Together with a type forward declaration, mutually recursive types
|
||||||
|
can be constructed such as:
|
||||||
|
|
||||||
|
type S2 // forward declaration of S2
|
||||||
|
type S1 struct { s2 *S2 }
|
||||||
|
type S2 struct { s1 *S1 }
|
||||||
|
|
||||||
|
By the end of the package source, all forward-declared types must be
|
||||||
|
fully declared if they are used.
|
||||||
|
|
||||||
There are no pointer literals.
|
There are no pointer literals.
|
||||||
|
|
||||||
@ -976,13 +983,17 @@ A constant declaration gives a name to the value of a constant expression.
|
|||||||
Type declarations
|
Type declarations
|
||||||
----
|
----
|
||||||
|
|
||||||
A type declaration introduces a name as a shorthand for a type.
|
A type declaration introduces a name as a shorthand for a type. Providing only
|
||||||
|
a name without a type serves as a forward declaration: The name is declared and
|
||||||
|
given an incomplete type. Incomplete types can be used together (and only) with
|
||||||
|
pointer types.
|
||||||
|
|
||||||
TypeDecl = "type" ( TypeSpec | "(" TypeSpecList [ ";" ] ")" ).
|
TypeDecl = "type" ( TypeSpec | "(" TypeSpecList [ ";" ] ")" ).
|
||||||
TypeSpec = identifier Type .
|
TypeSpec = identifier [ Type ] .
|
||||||
TypeSpecList = TypeSpec { ";" TypeSpec }.
|
TypeSpecList = TypeSpec { ";" TypeSpec }.
|
||||||
|
|
||||||
|
|
||||||
|
type List // foward declaration
|
||||||
type IntArray [16] int
|
type IntArray [16] int
|
||||||
type (
|
type (
|
||||||
Point struct { x, y float };
|
Point struct { x, y float };
|
||||||
|
Loading…
Reference in New Issue
Block a user