Skip to content

Commit b814011

Browse files
committed
update variable initializer
1 parent 24d9a70 commit b814011

File tree

4 files changed

+37
-4
lines changed

4 files changed

+37
-4
lines changed

doc/language/syntax/expression.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
[_name_](name.md) __|__
88
[_injected-variable_](injected_variable.md) __|__
99
[_positional-variable_](positional_variable.md) __|__
10-
[_variable-declaration_](variable_declaration.md) __|__
10+
[_variable-initializer_](variable_initializer.md) __|__
1111
[_operator-expression_](operator_expression.md) __|__
1212
[_if-control-expression_](if_control_expression.md) __|__
1313
[_while-control-expression_](while_control_expression.md) __|__

doc/language/syntax/statement.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Syntax
44

55
[_expression_statement_](expression_statement.md) __|__
6+
[_variable-declaration_](variable_declaration.md) __|__
67
[_return_](return.md) __|__
78
[_result_](result.md) __|__
89
[_yield_](yield.md) __|__

doc/language/syntax/variable_declaration.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## Syntax
44

5-
[_storage_class_](storage_class.md)__?__ [_binding_mode_](binding_mode.md) [_name_](name.md) _type_declaration_**?**
5+
[_storage_class_](storage_class.md)__?__ [_binding_mode_](binding_mode.md) [_name_](name.md) _type_declaration_**?** `;`
66

77
## Semantics
88
Declares a variable with a [_name_](name.md) which can be used in the scope or
@@ -14,8 +14,8 @@ Variable declarations may appear in the following contexts:
1414
- In a code block of a control-expression
1515
- In a class declaration.
1616

17-
A _variable_declaration_ is a [_expression_](expression.md) which results in a
18-
reference to declared variable.
17+
A _variable_declaration_ is a [_statement_](statement.md) as it was not assigned a value,
18+
there can be no result.
1919

2020
The type and coercion-method of the variable can be set by using the
2121
[_coerce_](coerce.md) operator on the _variable_declaration_ or on the
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# variable-declaration
2+
3+
## Syntax
4+
5+
[_storage_class_](storage_class.md)__?__ [_binding_mode_](binding_mode.md) [_name_](name.md) _type_declaration_**?** ( `=` | `+=` ) [_expression_](expression.md)
6+
7+
## Semantics
8+
Declares a variable with a [_name_](name.md) which can be used in the scope or
9+
child scopes where the variable is declared; and initializes it.
10+
11+
Variable declarations may appear in the following contexts:
12+
- In global scope.
13+
- In a function or lambda.
14+
- In a code block of a control-expression
15+
- In a class declaration.
16+
- In an expression.
17+
18+
A _variable_initializer_ is a [_expression_](expression.md) which results in a
19+
reference to declared variable.
20+
21+
The type and coercion-method of the variable can be set by using the
22+
[_coerce_](coerce.md) operator on the _variable_declaration_ or on the
23+
[_name_](name.md).
24+
25+
If the type is not explicitly set, the exact type of the first assigned value is
26+
used as the type of the variable. The coercion-method is `:=`, which means that
27+
any subsequent assignment to the variable must be of exactly the same type.
28+
29+
The assignment operator can be either:
30+
- `=` which assigns a value to the newly created variable, or
31+
- `+=` which assigns a value to a newly created variable or calls the `+=` operator
32+
an a existing variable of the same name and type.

0 commit comments

Comments
 (0)