From aff37662d1f70f2bf9e47b4f962e85521e7c18d1 Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Tue, 4 Oct 2016 10:06:14 -0700 Subject: [PATCH] spec: add new language for alias declarations For #16339. Change-Id: I7d912ea634bbfacfc0217f97dccb270fde06f16b Reviewed-on: https://go-review.googlesource.com/30601 Reviewed-by: Russ Cox --- doc/go_spec.html | 75 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 69 insertions(+), 6 deletions(-) diff --git a/doc/go_spec.html b/doc/go_spec.html index ee3a8457f2..6e745bc312 100644 --- a/doc/go_spec.html +++ b/doc/go_spec.html @@ -1,6 +1,6 @@ @@ -1726,8 +1726,12 @@ the left is bound to the value of the nth expression on the right.

+

+For constant aliases, see the section on alias declarations. +

+
-ConstDecl      = "const" ( ConstSpec | "(" { ConstSpec ";" } ")" ) .
+ConstDecl      = "const" ( ConstSpec | AliasSpec | "(" { ( ConstSpec | AliasSpec ) ";" } ")" ) .
 ConstSpec      = IdentifierList [ [ Type ] "=" ExpressionList ] .
 
 IdentifierList = identifier { "," identifier } .
@@ -1846,8 +1850,12 @@ and operations defined for the existing type are also defined for the new type.
 The new type is different from the existing type.
 

+

+For type aliases, see the section on alias declarations. +

+
-TypeDecl     = "type" ( TypeSpec | "(" { TypeSpec ";" } ")" ) .
+TypeDecl     = "type" ( TypeSpec | AliasSpec | "(" { ( TypeSpec | AliasSpec ) ";" } ")" ) .
 TypeSpec     = identifier Type .
 
@@ -1928,8 +1936,12 @@ A variable declaration creates one or more variables, binds corresponding identifiers to them, and gives each a type and an initial value.

+

+For variable aliases, see the section on alias declarations. +

+
-VarDecl     = "var" ( VarSpec | "(" { VarSpec ";" } ")" ) .
+VarDecl     = "var" ( VarSpec | AliasSpec | "(" { ( VarSpec | AliasSpec ) ";" } ")" ) .
 VarSpec     = IdentifierList ( Type [ "=" ExpressionList ] | "=" ExpressionList ) .
 
@@ -1976,6 +1988,7 @@ inside a function body if the variable is never used.

+

Short variable declarations

@@ -2006,7 +2019,7 @@ _, y, _ := coord(p) // coord() returns three values; only interested in y coord

Unlike regular variable declarations, a short variable declaration may redeclare variables provided they were originally declared earlier in the same block -(or the parameter lists if the block is the function body) with the same type, +(or the parameter lists if the block is the function body) with the same type, and at least one of the non-blank variables is new. As a consequence, redeclaration can only appear in a multi-variable short declaration. Redeclaration does not introduce a new variable; it just assigns a new value to the original. @@ -2034,8 +2047,12 @@ A function declaration binds an identifier, the function name, to a function.

+

+For function aliases, see the section on alias declarations. +

+
-FunctionDecl = "func" FunctionName ( Function | Signature ) .
+FunctionDecl = "func" ( FunctionName ( Function | Signature ) ) | AliasSpec .
 FunctionName = identifier .
 Function     = Signature FunctionBody .
 FunctionBody = Block .
@@ -2148,6 +2165,52 @@ However, a function declared this way is not a method.
 

+

Alias declarations

+ +

+An alias declaration binds an identifier, the alias, to a +constant, +type, +variable, or +function +denoted by a qualified identifier and +declared in a different package. +

+ +
+AliasSpec = identifier "=>" QualifiedIdent .
+
+ +

+The effect of referring to a constant, type, variable, or function by an alias +is indistinguishable from referring to it by its original name. +For example, the type denoted by a type alias and the aliased type are +identical. +

+ +

+An alias declaration may appear only as a form of constant, type, variable, +or function declaration at the package level, and the aliased entity must be +a constant, type, variable, or function respectively. Alias declarations inside +functions are not permitted. +

+ +
+const (
+	G  =  6.67408e-11      // regular and alias declarations may be grouped
+	Pi => math.Pi          // same effect as: Pi = math.Pi
+)
+
+type Struct => types.Struct    // re-export of types.Struct
+
+func sin => math.Sin           // non-exported shortcut for frequently used function
+
+ +

+An alias declaration may not refer to package unsafe. +

+ +

Expressions

-- 2.48.1