2018-04-17 15:16:08 -04:00
|
|
|
[[painless-variables]]
|
2017-05-12 19:17:06 -04:00
|
|
|
=== Variables
|
|
|
|
|
2018-05-23 16:36:58 -04:00
|
|
|
A variable loads and stores a value for evaluation during
|
|
|
|
<<painless-operators, operations>>.
|
2017-05-12 19:17:06 -04:00
|
|
|
|
Painless: Restructure/Clean Up of Spec Documentation (#31013)
Full restructure of the spec into new sections for operators, statements, scripts, functions, lambdas, and regexes. Split of operators into 6 sections, a table, reference, array, numeric, boolean, and general. Clean up of all operators sections. Sporadic clean up else where.
2018-06-07 20:11:56 -04:00
|
|
|
[[variable-declaration]]
|
2018-04-17 15:16:08 -04:00
|
|
|
==== Declaration
|
2017-05-12 19:17:06 -04:00
|
|
|
|
2018-05-23 16:36:58 -04:00
|
|
|
Declare a variable before use with the format of <<painless-types, type>>
|
|
|
|
followed by <<painless-identifiers, identifier>>. Declare an
|
|
|
|
<<array-type, array type>> variable using an opening `[` token and a closing `]`
|
|
|
|
token for each dimension directly after the identifier. Specify a
|
|
|
|
comma-separated list of identifiers following the type to declare multiple
|
Painless: Restructure/Clean Up of Spec Documentation (#31013)
Full restructure of the spec into new sections for operators, statements, scripts, functions, lambdas, and regexes. Split of operators into 6 sections, a table, reference, array, numeric, boolean, and general. Clean up of all operators sections. Sporadic clean up else where.
2018-06-07 20:11:56 -04:00
|
|
|
variables in a single statement. Use an
|
|
|
|
<<variable-assignment, assignment operator>> combined with a declaration to
|
|
|
|
immediately assign a value to a variable. A variable not immediately assigned a
|
|
|
|
value will have a default value assigned implicitly based on the type.
|
2018-05-23 16:36:58 -04:00
|
|
|
|
|
|
|
*Errors*
|
|
|
|
|
|
|
|
* If a variable is used prior to or without declaration.
|
2017-05-12 19:17:06 -04:00
|
|
|
|
2018-04-25 12:38:41 -04:00
|
|
|
*Grammar*
|
Painless: Restructure/Clean Up of Spec Documentation (#31013)
Full restructure of the spec into new sections for operators, statements, scripts, functions, lambdas, and regexes. Split of operators into 6 sections, a table, reference, array, numeric, boolean, and general. Clean up of all operators sections. Sporadic clean up else where.
2018-06-07 20:11:56 -04:00
|
|
|
|
2017-05-12 19:17:06 -04:00
|
|
|
[source,ANTLR4]
|
|
|
|
----
|
2018-04-25 12:38:41 -04:00
|
|
|
declaration : type ID assignment? (',' ID assignment?)*;
|
2018-05-23 16:36:58 -04:00
|
|
|
type: ID ('.' ID)* ('[' ']')*;
|
2018-04-25 12:38:41 -04:00
|
|
|
assignment: '=' expression;
|
2017-05-12 19:17:06 -04:00
|
|
|
----
|
|
|
|
|
2018-04-25 12:38:41 -04:00
|
|
|
*Examples*
|
|
|
|
|
|
|
|
* Different variations of variable declaration.
|
|
|
|
+
|
|
|
|
[source,Painless]
|
2017-05-12 19:17:06 -04:00
|
|
|
----
|
2018-04-25 12:38:41 -04:00
|
|
|
<1> int x;
|
|
|
|
<2> List y;
|
2018-05-23 16:36:58 -04:00
|
|
|
<3> int x, y = 5, z;
|
|
|
|
<4> def d;
|
2018-04-25 12:38:41 -04:00
|
|
|
<5> int i = 10;
|
2018-05-23 16:36:58 -04:00
|
|
|
<6> float[] f;
|
|
|
|
<7> Map[][] m;
|
|
|
|
----
|
|
|
|
+
|
|
|
|
<1> declare `int x`;
|
Painless: Restructure/Clean Up of Spec Documentation (#31013)
Full restructure of the spec into new sections for operators, statements, scripts, functions, lambdas, and regexes. Split of operators into 6 sections, a table, reference, array, numeric, boolean, and general. Clean up of all operators sections. Sporadic clean up else where.
2018-06-07 20:11:56 -04:00
|
|
|
store default `null` to `x`
|
2018-05-23 16:36:58 -04:00
|
|
|
<2> declare `List y`;
|
Painless: Restructure/Clean Up of Spec Documentation (#31013)
Full restructure of the spec into new sections for operators, statements, scripts, functions, lambdas, and regexes. Split of operators into 6 sections, a table, reference, array, numeric, boolean, and general. Clean up of all operators sections. Sporadic clean up else where.
2018-06-07 20:11:56 -04:00
|
|
|
store default `null` to `y`
|
2018-05-23 16:36:58 -04:00
|
|
|
<3> declare `int x`;
|
Painless: Restructure/Clean Up of Spec Documentation (#31013)
Full restructure of the spec into new sections for operators, statements, scripts, functions, lambdas, and regexes. Split of operators into 6 sections, a table, reference, array, numeric, boolean, and general. Clean up of all operators sections. Sporadic clean up else where.
2018-06-07 20:11:56 -04:00
|
|
|
store default `int 0` to `x`;
|
2018-05-23 16:36:58 -04:00
|
|
|
declare `int y`;
|
Painless: Restructure/Clean Up of Spec Documentation (#31013)
Full restructure of the spec into new sections for operators, statements, scripts, functions, lambdas, and regexes. Split of operators into 6 sections, a table, reference, array, numeric, boolean, and general. Clean up of all operators sections. Sporadic clean up else where.
2018-06-07 20:11:56 -04:00
|
|
|
store `int 5` to `y`;
|
2018-05-23 16:36:58 -04:00
|
|
|
declare `int z`;
|
Painless: Restructure/Clean Up of Spec Documentation (#31013)
Full restructure of the spec into new sections for operators, statements, scripts, functions, lambdas, and regexes. Split of operators into 6 sections, a table, reference, array, numeric, boolean, and general. Clean up of all operators sections. Sporadic clean up else where.
2018-06-07 20:11:56 -04:00
|
|
|
store default `int 0` to `z`;
|
2018-05-23 16:36:58 -04:00
|
|
|
<4> declare `def d`;
|
Painless: Restructure/Clean Up of Spec Documentation (#31013)
Full restructure of the spec into new sections for operators, statements, scripts, functions, lambdas, and regexes. Split of operators into 6 sections, a table, reference, array, numeric, boolean, and general. Clean up of all operators sections. Sporadic clean up else where.
2018-06-07 20:11:56 -04:00
|
|
|
store default `null` to `d`
|
2018-05-23 16:36:58 -04:00
|
|
|
<5> declare `int i`;
|
Painless: Restructure/Clean Up of Spec Documentation (#31013)
Full restructure of the spec into new sections for operators, statements, scripts, functions, lambdas, and regexes. Split of operators into 6 sections, a table, reference, array, numeric, boolean, and general. Clean up of all operators sections. Sporadic clean up else where.
2018-06-07 20:11:56 -04:00
|
|
|
store `int 10` to `i`
|
2018-05-23 16:36:58 -04:00
|
|
|
<6> declare `float[] f`;
|
Painless: Restructure/Clean Up of Spec Documentation (#31013)
Full restructure of the spec into new sections for operators, statements, scripts, functions, lambdas, and regexes. Split of operators into 6 sections, a table, reference, array, numeric, boolean, and general. Clean up of all operators sections. Sporadic clean up else where.
2018-06-07 20:11:56 -04:00
|
|
|
store default `null` to `f`
|
2018-05-23 16:36:58 -04:00
|
|
|
<7> declare `Map[][] m`;
|
Painless: Restructure/Clean Up of Spec Documentation (#31013)
Full restructure of the spec into new sections for operators, statements, scripts, functions, lambdas, and regexes. Split of operators into 6 sections, a table, reference, array, numeric, boolean, and general. Clean up of all operators sections. Sporadic clean up else where.
2018-06-07 20:11:56 -04:00
|
|
|
store default `null` to `m`
|
2017-05-12 19:17:06 -04:00
|
|
|
|
Painless: Restructure/Clean Up of Spec Documentation (#31013)
Full restructure of the spec into new sections for operators, statements, scripts, functions, lambdas, and regexes. Split of operators into 6 sections, a table, reference, array, numeric, boolean, and general. Clean up of all operators sections. Sporadic clean up else where.
2018-06-07 20:11:56 -04:00
|
|
|
[[variable-assignment]]
|
2018-04-17 15:16:08 -04:00
|
|
|
==== Assignment
|
2017-05-12 19:17:06 -04:00
|
|
|
|
Painless: Restructure/Clean Up of Spec Documentation (#31013)
Full restructure of the spec into new sections for operators, statements, scripts, functions, lambdas, and regexes. Split of operators into 6 sections, a table, reference, array, numeric, boolean, and general. Clean up of all operators sections. Sporadic clean up else where.
2018-06-07 20:11:56 -04:00
|
|
|
Use the `assignment operator '='` to store a value in a variable for use in
|
|
|
|
subsequent operations. Any operation that produces a value can be assigned to
|
|
|
|
any variable as long as the <<painless-types, types>> are the same or the
|
|
|
|
resultant type can be <<painless-casting, implicitly cast>> to the variable
|
|
|
|
type.
|
2018-05-23 16:36:58 -04:00
|
|
|
|
|
|
|
*Errors*
|
|
|
|
|
|
|
|
* If the type of value is unable to match the type of variable.
|
2017-05-12 19:17:06 -04:00
|
|
|
|
2018-04-25 12:38:41 -04:00
|
|
|
*Grammar*
|
Painless: Restructure/Clean Up of Spec Documentation (#31013)
Full restructure of the spec into new sections for operators, statements, scripts, functions, lambdas, and regexes. Split of operators into 6 sections, a table, reference, array, numeric, boolean, and general. Clean up of all operators sections. Sporadic clean up else where.
2018-06-07 20:11:56 -04:00
|
|
|
|
2017-05-12 19:17:06 -04:00
|
|
|
[source,ANTLR4]
|
|
|
|
----
|
|
|
|
assignment: ID '=' expression
|
|
|
|
----
|
|
|
|
|
2018-04-25 12:38:41 -04:00
|
|
|
*Examples*
|
|
|
|
|
2018-05-23 16:36:58 -04:00
|
|
|
* Variable assignment with an integer literal.
|
2018-04-25 12:38:41 -04:00
|
|
|
+
|
|
|
|
[source,Painless]
|
|
|
|
----
|
|
|
|
<1> int i;
|
|
|
|
<2> i = 10;
|
|
|
|
----
|
|
|
|
+
|
2018-05-23 16:36:58 -04:00
|
|
|
<1> declare `int i`;
|
Painless: Restructure/Clean Up of Spec Documentation (#31013)
Full restructure of the spec into new sections for operators, statements, scripts, functions, lambdas, and regexes. Split of operators into 6 sections, a table, reference, array, numeric, boolean, and general. Clean up of all operators sections. Sporadic clean up else where.
2018-06-07 20:11:56 -04:00
|
|
|
store default `int 0` to `i`
|
|
|
|
<2> store `int 10` to `i`
|
2018-04-25 12:38:41 -04:00
|
|
|
+
|
2018-05-23 16:36:58 -04:00
|
|
|
* Declaration combined with immediate assignment.
|
2018-04-25 12:38:41 -04:00
|
|
|
+
|
|
|
|
[source,Painless]
|
|
|
|
----
|
|
|
|
<1> int i = 10;
|
|
|
|
<2> double j = 2.0;
|
|
|
|
----
|
|
|
|
+
|
2018-05-23 16:36:58 -04:00
|
|
|
<1> declare `int i`;
|
Painless: Restructure/Clean Up of Spec Documentation (#31013)
Full restructure of the spec into new sections for operators, statements, scripts, functions, lambdas, and regexes. Split of operators into 6 sections, a table, reference, array, numeric, boolean, and general. Clean up of all operators sections. Sporadic clean up else where.
2018-06-07 20:11:56 -04:00
|
|
|
store `int 10` to `i`
|
2018-05-23 16:36:58 -04:00
|
|
|
<2> declare `double j`;
|
Painless: Restructure/Clean Up of Spec Documentation (#31013)
Full restructure of the spec into new sections for operators, statements, scripts, functions, lambdas, and regexes. Split of operators into 6 sections, a table, reference, array, numeric, boolean, and general. Clean up of all operators sections. Sporadic clean up else where.
2018-06-07 20:11:56 -04:00
|
|
|
store `double 2.0` to `j`
|
2018-04-25 12:38:41 -04:00
|
|
|
+
|
Painless: Restructure/Clean Up of Spec Documentation (#31013)
Full restructure of the spec into new sections for operators, statements, scripts, functions, lambdas, and regexes. Split of operators into 6 sections, a table, reference, array, numeric, boolean, and general. Clean up of all operators sections. Sporadic clean up else where.
2018-06-07 20:11:56 -04:00
|
|
|
* Assignment of one variable to another using primitive type values.
|
2018-04-25 12:38:41 -04:00
|
|
|
+
|
|
|
|
[source,Painless]
|
|
|
|
----
|
|
|
|
<1> int i = 10;
|
|
|
|
<2> int j = i;
|
|
|
|
----
|
|
|
|
+
|
2018-05-23 16:36:58 -04:00
|
|
|
<1> declare `int i`;
|
Painless: Restructure/Clean Up of Spec Documentation (#31013)
Full restructure of the spec into new sections for operators, statements, scripts, functions, lambdas, and regexes. Split of operators into 6 sections, a table, reference, array, numeric, boolean, and general. Clean up of all operators sections. Sporadic clean up else where.
2018-06-07 20:11:56 -04:00
|
|
|
store `int 10` to `i`
|
2018-05-23 16:36:58 -04:00
|
|
|
<2> declare `int j`;
|
Painless: Restructure/Clean Up of Spec Documentation (#31013)
Full restructure of the spec into new sections for operators, statements, scripts, functions, lambdas, and regexes. Split of operators into 6 sections, a table, reference, array, numeric, boolean, and general. Clean up of all operators sections. Sporadic clean up else where.
2018-06-07 20:11:56 -04:00
|
|
|
load from `i` -> `int 10`;
|
|
|
|
store `int 10` to `j`
|
2018-04-25 12:38:41 -04:00
|
|
|
+
|
Painless: Restructure/Clean Up of Spec Documentation (#31013)
Full restructure of the spec into new sections for operators, statements, scripts, functions, lambdas, and regexes. Split of operators into 6 sections, a table, reference, array, numeric, boolean, and general. Clean up of all operators sections. Sporadic clean up else where.
2018-06-07 20:11:56 -04:00
|
|
|
* Assignment with reference types using the
|
|
|
|
<<new-instance-operator, new instance operator>>.
|
2018-04-25 12:38:41 -04:00
|
|
|
+
|
|
|
|
[source,Painless]
|
|
|
|
----
|
|
|
|
<1> ArrayList l = new ArrayList();
|
|
|
|
<2> Map m = new HashMap();
|
|
|
|
----
|
|
|
|
+
|
2018-05-23 16:36:58 -04:00
|
|
|
<1> declare `ArrayList l`;
|
|
|
|
allocate `ArrayList` instance -> `ArrayList reference`;
|
Painless: Restructure/Clean Up of Spec Documentation (#31013)
Full restructure of the spec into new sections for operators, statements, scripts, functions, lambdas, and regexes. Split of operators into 6 sections, a table, reference, array, numeric, boolean, and general. Clean up of all operators sections. Sporadic clean up else where.
2018-06-07 20:11:56 -04:00
|
|
|
store `ArrayList reference` to `l`
|
2018-05-23 16:36:58 -04:00
|
|
|
<2> declare `Map m`;
|
|
|
|
allocate `HashMap` instance -> `HashMap reference`;
|
|
|
|
implicit cast `HashMap reference` to `Map reference` -> `Map reference`;
|
Painless: Restructure/Clean Up of Spec Documentation (#31013)
Full restructure of the spec into new sections for operators, statements, scripts, functions, lambdas, and regexes. Split of operators into 6 sections, a table, reference, array, numeric, boolean, and general. Clean up of all operators sections. Sporadic clean up else where.
2018-06-07 20:11:56 -04:00
|
|
|
store `Map reference` to `m`
|
2018-04-25 12:38:41 -04:00
|
|
|
+
|
Painless: Restructure/Clean Up of Spec Documentation (#31013)
Full restructure of the spec into new sections for operators, statements, scripts, functions, lambdas, and regexes. Split of operators into 6 sections, a table, reference, array, numeric, boolean, and general. Clean up of all operators sections. Sporadic clean up else where.
2018-06-07 20:11:56 -04:00
|
|
|
* Assignment of one variable to another using reference type values.
|
2018-04-25 12:38:41 -04:00
|
|
|
+
|
|
|
|
[source,Painless]
|
|
|
|
----
|
|
|
|
<1> List l = new ArrayList();
|
|
|
|
<2> List k = l;
|
|
|
|
<3> List m;
|
|
|
|
<4> m = k;
|
|
|
|
----
|
|
|
|
+
|
2018-05-23 16:36:58 -04:00
|
|
|
<1> declare `List l`;
|
|
|
|
allocate `ArrayList` instance -> `ArrayList reference`;
|
|
|
|
implicit cast `ArrayList reference` to `List reference` -> `List reference`;
|
Painless: Restructure/Clean Up of Spec Documentation (#31013)
Full restructure of the spec into new sections for operators, statements, scripts, functions, lambdas, and regexes. Split of operators into 6 sections, a table, reference, array, numeric, boolean, and general. Clean up of all operators sections. Sporadic clean up else where.
2018-06-07 20:11:56 -04:00
|
|
|
store `List reference` to `l`
|
2018-05-23 16:36:58 -04:00
|
|
|
<2> declare `List k`;
|
Painless: Restructure/Clean Up of Spec Documentation (#31013)
Full restructure of the spec into new sections for operators, statements, scripts, functions, lambdas, and regexes. Split of operators into 6 sections, a table, reference, array, numeric, boolean, and general. Clean up of all operators sections. Sporadic clean up else where.
2018-06-07 20:11:56 -04:00
|
|
|
load from `l` -> `List reference`;
|
|
|
|
store `List reference` to `k`;
|
2018-05-23 16:36:58 -04:00
|
|
|
(note `l` and `k` refer to the same instance known as a shallow-copy)
|
2018-04-25 12:38:41 -04:00
|
|
|
<3> declare `List m`;
|
Painless: Restructure/Clean Up of Spec Documentation (#31013)
Full restructure of the spec into new sections for operators, statements, scripts, functions, lambdas, and regexes. Split of operators into 6 sections, a table, reference, array, numeric, boolean, and general. Clean up of all operators sections. Sporadic clean up else where.
2018-06-07 20:11:56 -04:00
|
|
|
store default `null` to `m`
|
|
|
|
<4> load from `k` -> `List reference`;
|
|
|
|
store `List reference` to `m`;
|
2018-05-23 16:36:58 -04:00
|
|
|
(note `l`, `k`, and `m` refer to the same instance)
|
|
|
|
+
|
Painless: Restructure/Clean Up of Spec Documentation (#31013)
Full restructure of the spec into new sections for operators, statements, scripts, functions, lambdas, and regexes. Split of operators into 6 sections, a table, reference, array, numeric, boolean, and general. Clean up of all operators sections. Sporadic clean up else where.
2018-06-07 20:11:56 -04:00
|
|
|
* Assignment with array type variables using the
|
|
|
|
<<new-array-operator, new array operator>>.
|
2018-05-23 16:36:58 -04:00
|
|
|
+
|
|
|
|
[source,Painless]
|
|
|
|
----
|
|
|
|
<1> int[] ia1;
|
|
|
|
<2> ia1 = new int[2];
|
|
|
|
<3> ia1[0] = 1;
|
|
|
|
<4> int[] ib1 = ia1;
|
|
|
|
<5> int[][] ic2 = new int[2][5];
|
|
|
|
<6> ic2[1][3] = 2;
|
|
|
|
<7> ic2[0] = ia1;
|
|
|
|
----
|
|
|
|
+
|
|
|
|
<1> declare `int[] ia1`;
|
Painless: Restructure/Clean Up of Spec Documentation (#31013)
Full restructure of the spec into new sections for operators, statements, scripts, functions, lambdas, and regexes. Split of operators into 6 sections, a table, reference, array, numeric, boolean, and general. Clean up of all operators sections. Sporadic clean up else where.
2018-06-07 20:11:56 -04:00
|
|
|
store default `null` to `ia1`
|
2018-05-23 16:36:58 -04:00
|
|
|
<2> allocate `1-d int array` instance with `length [2]`
|
|
|
|
-> `1-d int array reference`;
|
Painless: Restructure/Clean Up of Spec Documentation (#31013)
Full restructure of the spec into new sections for operators, statements, scripts, functions, lambdas, and regexes. Split of operators into 6 sections, a table, reference, array, numeric, boolean, and general. Clean up of all operators sections. Sporadic clean up else where.
2018-06-07 20:11:56 -04:00
|
|
|
store `1-d int array reference` to `ia1`
|
|
|
|
<3> load from `ia1` -> `1-d int array reference`;
|
|
|
|
store `int 1` to `index [0]` of `1-d int array reference`
|
2018-05-23 16:36:58 -04:00
|
|
|
<4> declare `int[] ib1`;
|
Painless: Restructure/Clean Up of Spec Documentation (#31013)
Full restructure of the spec into new sections for operators, statements, scripts, functions, lambdas, and regexes. Split of operators into 6 sections, a table, reference, array, numeric, boolean, and general. Clean up of all operators sections. Sporadic clean up else where.
2018-06-07 20:11:56 -04:00
|
|
|
load from `ia1` -> `1-d int array reference`;
|
|
|
|
store `1-d int array reference` to `ib1`;
|
2018-05-23 16:36:58 -04:00
|
|
|
(note `ia1` and `ib1` refer to the same instance known as a shallow copy)
|
|
|
|
<5> declare `int[][] ic2`;
|
|
|
|
allocate `2-d int array` instance with `length [2, 5]`
|
|
|
|
-> `2-d int array reference`;
|
Painless: Restructure/Clean Up of Spec Documentation (#31013)
Full restructure of the spec into new sections for operators, statements, scripts, functions, lambdas, and regexes. Split of operators into 6 sections, a table, reference, array, numeric, boolean, and general. Clean up of all operators sections. Sporadic clean up else where.
2018-06-07 20:11:56 -04:00
|
|
|
store `2-d int array reference` to `ic2`
|
|
|
|
<6> load from `ic2` -> `2-d int array reference`;
|
|
|
|
store `int 2` to `index [1, 3]` of `2-d int array reference`
|
|
|
|
<7> load from `ia1` -> `1-d int array reference`;
|
|
|
|
load from `ic2` -> `2-d int array reference`;
|
|
|
|
store `1-d int array reference` to
|
2018-05-23 16:36:58 -04:00
|
|
|
`index [0]` of `2-d int array reference`;
|
|
|
|
(note `ia1`, `ib1`, and `index [0]` of `ia2` refer to the same instance)
|