mirror of
https://github.com/golang/go
synced 2024-11-25 01:17:56 -07:00
- clarify type declaration (specifying status quo)
DELTA=68 (51 added, 8 deleted, 9 changed) OCL=35038 CL=35046
This commit is contained in:
parent
7743ffead4
commit
fc61b77754
@ -57,13 +57,6 @@ and convert §Foo into §<a href="#Foo">Foo</a>:
|
|||||||
<div id="nav"></div>
|
<div id="nav"></div>
|
||||||
|
|
||||||
<!--
|
<!--
|
||||||
Open issues:
|
|
||||||
[ ] Semantics of type declaration:
|
|
||||||
- creating a new type (status quo), or only a new type name?
|
|
||||||
- declaration "type T S" strips methods of S. why/why not?
|
|
||||||
- no mechanism to declare a local type name: type T P.T
|
|
||||||
|
|
||||||
|
|
||||||
Todo
|
Todo
|
||||||
[ ] clarify: two equal lowercase identifiers from different packages denote different objects
|
[ ] clarify: two equal lowercase identifiers from different packages denote different objects
|
||||||
[ ] need language about function/method calls and parameter passing rules
|
[ ] need language about function/method calls and parameter passing rules
|
||||||
@ -539,8 +532,11 @@ TypeLit = ArrayType | StructType | PointerType | FunctionType | InterfaceType
|
|||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
Named instances of the boolean, numeric, and string types are <a href="#Predeclared_identifiers">predeclared</a>.
|
Named instances of the boolean, numeric, and string types are
|
||||||
Array, struct, pointer, function, interface, slice, map, and channel types may be constructed using type literals.
|
<a href="#Predeclared_identifiers">predeclared</a>.
|
||||||
|
<i>Composite types</i>—array, struct, pointer, function,
|
||||||
|
interface, slice, map, and channel types—may be constructed using
|
||||||
|
type literals.
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
@ -548,8 +544,7 @@ A type may have a <i>method set</i> associated with it
|
|||||||
(§<a href="#Interface_types">Interface types</a>, §<a href="#Method_declarations">Method declarations</a>).
|
(§<a href="#Interface_types">Interface types</a>, §<a href="#Method_declarations">Method declarations</a>).
|
||||||
The method set of an <a href="#Interface_types">interface type</a> is its interface.
|
The method set of an <a href="#Interface_types">interface type</a> is its interface.
|
||||||
The method set of any other named type <code>T</code>
|
The method set of any other named type <code>T</code>
|
||||||
consists of all methods with receiver
|
consists of all methods with receiver type <code>T</code>.
|
||||||
type <code>T</code>.
|
|
||||||
The method set of the corresponding pointer type <code>*T</code>
|
The method set of the corresponding pointer type <code>*T</code>
|
||||||
is the set of all methods with receiver <code>*T</code> or <code>T</code>
|
is the set of all methods with receiver <code>*T</code> or <code>T</code>
|
||||||
(that is, it also contains the method set of <code>T</code>).
|
(that is, it also contains the method set of <code>T</code>).
|
||||||
@ -634,7 +629,7 @@ The predeclared string type is <code>string</code>.
|
|||||||
|
|
||||||
<p>
|
<p>
|
||||||
The elements of strings have type <code>byte</code> and may be
|
The elements of strings have type <code>byte</code> and may be
|
||||||
accessed using the usual indexing operations (§<a href="#Indexes">Indexes</a>). It is
|
accessed using the usual <a href="#Indexes">indexing operations</a>. It is
|
||||||
illegal to take the address of such an element; if
|
illegal to take the address of such an element; if
|
||||||
<code>s[i]</code> is the <i>i</i>th byte of a
|
<code>s[i]</code> is the <i>i</i>th byte of a
|
||||||
string, <code>&s[i]</code> is invalid. The length of string
|
string, <code>&s[i]</code> is invalid. The length of string
|
||||||
@ -1130,8 +1125,9 @@ received, <code>closed(c)</code> returns true.
|
|||||||
<h2 id="Properties_of_types_and_values">Properties of types and values</h2>
|
<h2 id="Properties_of_types_and_values">Properties of types and values</h2>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
Two types may be <i>identical</i>, <i>compatible</i>, or <i>incompatible</i>.
|
Two types are either <i>identical</i> or <i>different</i>, and they are
|
||||||
Two identical types are always compatible, but two compatible types may not be identical.
|
either <i>compatible</i> or <i>incompatible</i>.
|
||||||
|
Identical types are always compatible, but compatible types need not be identical.
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<h3 id="Type_identity_and_compatibility">Type identity and compatibility</h3>
|
<h3 id="Type_identity_and_compatibility">Type identity and compatibility</h3>
|
||||||
@ -1609,8 +1605,11 @@ last non-empty expression list.
|
|||||||
<h3 id="Type_declarations">Type declarations</h3>
|
<h3 id="Type_declarations">Type declarations</h3>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
A type declaration binds an identifier, the <i>type name</i>,
|
A type declaration binds an identifier, the <i>type name</i>, to a new type
|
||||||
to a new type. <font color=red>TODO: what exactly is a "new type"?</font>
|
that has the same definition (element, fields, channel direction, etc.) as
|
||||||
|
an existing type. The new type is
|
||||||
|
<a href="#Properties_of_types_and_values">compatible</a> with, but
|
||||||
|
<a href="#Properties_of_types_and_values">different</a> from, the existing type.
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<pre class="ebnf">
|
<pre class="ebnf">
|
||||||
@ -1639,6 +1638,49 @@ type Cipher interface {
|
|||||||
}
|
}
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
|
<p>
|
||||||
|
The declared type does not inherit any <a href="#Method_declarations">methods</a>
|
||||||
|
bound to the existing type, but the <a href="#Types">method set</a>
|
||||||
|
of elements of a composite type is not changed:
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
// A Mutex is a data type with two methods Lock and Unlock.
|
||||||
|
type Mutex struct { /* Mutex fields */ }
|
||||||
|
func (m *Mutex) Lock() { /* Lock implementation */ }
|
||||||
|
func (m *Mutex) Unlock() { /* Unlock implementation */ }
|
||||||
|
|
||||||
|
// NewMutex has the same composition as Mutex but its method set is empty.
|
||||||
|
type NewMutex Mutex
|
||||||
|
|
||||||
|
// PrintableMutex has no methods bound to it, but the method set contains
|
||||||
|
// the methods Lock and Unlock bound to its anonymous field Mutex.
|
||||||
|
type PrintableMutex struct {
|
||||||
|
Mutex;
|
||||||
|
}
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p>
|
||||||
|
A type declaration may be used to define a different boolean, numeric, or string
|
||||||
|
type and attach methods to it:
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
type TimeZone int
|
||||||
|
|
||||||
|
const (
|
||||||
|
EST TimeZone = -(5 + iota);
|
||||||
|
CST;
|
||||||
|
MST;
|
||||||
|
PST;
|
||||||
|
)
|
||||||
|
|
||||||
|
func (tz TimeZone) String() string {
|
||||||
|
return fmt.Sprintf("GMT+%dh", tz);
|
||||||
|
}
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
|
||||||
<h3 id="Variable_declarations">Variable declarations</h3>
|
<h3 id="Variable_declarations">Variable declarations</h3>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
@ -1775,8 +1817,9 @@ which is a function with a <i>receiver</i>.
|
|||||||
</p>
|
</p>
|
||||||
<pre class="ebnf">
|
<pre class="ebnf">
|
||||||
MethodDecl = "func" Receiver MethodName Signature [ Body ] .
|
MethodDecl = "func" Receiver MethodName Signature [ Body ] .
|
||||||
Receiver = "(" [ identifier ] [ "*" ] TypeName ")" .
|
Receiver = "(" [ identifier ] [ "*" ] BaseTypeName ")" .
|
||||||
MethodName = identifier .
|
MethodName = identifier .
|
||||||
|
BaseTypeName = identifier .
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
|
Loading…
Reference in New Issue
Block a user