Skip to content

Commit b905ef1

Browse files
committed
Painless: Types Section Clean Up (#30283)
Clean up of types section, casting section, and a large number of examples.
1 parent 07e830e commit b905ef1

9 files changed

+1032
-459
lines changed

docs/painless/painless-casting.asciidoc

Lines changed: 417 additions & 133 deletions
Large diffs are not rendered by default.

docs/painless/painless-comments.asciidoc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
[[painless-comments]]
22
=== Comments
33

4-
Use the `//` token anywhere on a line to specify a single-line comment. All
5-
characters from the `//` token to the end of the line are ignored. Use an
6-
opening `/*` token and a closing `*/` token to specify a multi-line comment.
7-
Multi-line comments can start anywhere on a line, and all characters in between
8-
the `/*` token and `*/` token are ignored. Comments can be included anywhere
9-
within a script.
4+
Use a comment to annotate or explain code within a script. Use the `//` token
5+
anywhere on a line to specify a single-line comment. All characters from the
6+
`//` token to the end of the line are ignored. Use an opening `/*` token and a
7+
closing `*/` token to specify a multi-line comment. Multi-line comments can
8+
start anywhere on a line, and all characters in between the `/*` token and `*/`
9+
token are ignored. Comments can be included anywhere within a script.
1010

1111
*Grammar*
1212
[source,ANTLR4]

docs/painless/painless-identifiers.asciidoc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
[[painless-identifiers]]
22
=== Identifiers
33

4-
Specify identifiers to <<declaration, declare>>, <<assignment, assign>>, and
5-
<<painless-operators, use>> variables, <<dot-operator, access fields>>, and
6-
<<dot-operator, call methods>>. <<painless-keywords, Keywords>> and
7-
<<painless-types, types>> cannot be used as identifiers.
4+
Use an identifier as a named token to specify a
5+
<<painless-variables, variable>>, <<painless-types, type>>,
6+
<<dot-operator, field>>, <<dot-operator, method>>, or function.
7+
<<painless-keywords, Keywords>> cannot be used as identifiers.
88

99
*Grammar*
1010
[source,ANTLR4]

docs/painless/painless-keywords.asciidoc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
[[painless-keywords]]
22
=== Keywords
33

4-
The keywords in the table below are reserved for built-in language
5-
features. These keywords cannot be used as
6-
<<painless-identifiers, identifiers>> or <<painless-types, types>>.
4+
Keywords are reserved tokens for built-in language features and cannot be used
5+
as <<painless-identifiers, identifiers>> within a script. The following are
6+
keywords:
77

88
[cols="^1,^1,^1,^1,^1"]
99
|====

docs/painless/painless-lang-spec.asciidoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Painless syntax is similar to Java syntax along with some additional
66
features such as dynamic typing, Map and List accessor shortcuts, and array
77
initializers. As a direct comparison to Java, there are some important
88
differences, especially related to the casting model. For more detailed
9-
conceptual information about the basic constructs that Java and Painless share,
9+
conceptual information about the basic constructs that Painless and Java share,
1010
refer to the corresponding topics in the
1111
https://docs.oracle.com/javase/specs/jls/se8/html/index.html[Java Language
1212
Specification].

docs/painless/painless-literals.asciidoc

Lines changed: 16 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
[[painless-literals]]
22
=== Literals
33

4-
Use literals to specify different types of values directly in a script.
4+
Use a literal to specify a value directly in an
5+
<<painless-operators, operation>>.
56

67
[[integers]]
78
==== Integers
89

9-
Use integer literals to specify an integer value in decimal, octal, or hex
10-
notation of the <<primitive-types, primitive types>> `int`, `long`, `float`,
10+
Use an integer literal to specify an integer type value in decimal, octal, or
11+
hex notation of a <<primitive-types, primitive type>> `int`, `long`, `float`,
1112
or `double`. Use the following single letter designations to specify the
12-
<<primitive-types, primitive type>>: `l` or `L` for `long`, `f` or `F` for
13-
`float`, and `d` or `D` for `double`. If not specified, the type defaults to
14-
`int`. Use `0` as a prefix to specify an integer literal as octal, and use
15-
`0x` or `0X` as a prefix to specify an integer literal as hex.
13+
primitive type: `l` or `L` for `long`, `f` or `F` for `float`, and `d` or `D`
14+
for `double`. If not specified, the type defaults to `int`. Use `0` as a prefix
15+
to specify an integer literal as octal, and use `0x` or `0X` as a prefix to
16+
specify an integer literal as hex.
1617

1718
*Grammar*
1819
[source,ANTLR4]
@@ -46,11 +47,10 @@ HEX: '-'? '0' [xX] [0-9a-fA-F]+ [lL]?;
4647
[[floats]]
4748
==== Floats
4849

49-
Use floating point literals to specify a floating point value of the
50-
<<primitive-types, primitive types>> `float` or `double`. Use the following
51-
single letter designations to specify the <<primitive-types, primitive type>>:
52-
`f` or `F` for `float` and `d` or `D` for `double`. If not specified, the type defaults
53-
to `double`.
50+
Use a floating point literal to specify a floating point type value of a
51+
<<primitive-types, primitive type>> `float` or `double`. Use the following
52+
single letter designations to specify the primitive type: `f` or `F` for `float`
53+
and `d` or `D` for `double`. If not specified, the type defaults to `double`.
5454

5555
*Grammar*
5656
[source,ANTLR4]
@@ -81,7 +81,7 @@ EXPONENT: ( [eE] [+\-]? [0-9]+ );
8181
[[strings]]
8282
==== Strings
8383

84-
Use string literals to specify <<string-type, String>> values with
84+
Use a string literal to specify a <<string-type, `String` type>> value with
8585
either single-quotes or double-quotes. Use a `\"` token to include a
8686
double-quote as part of a double-quoted string literal. Use a `\'` token to
8787
include a single-quote as part of a single-quoted string literal. Use a `\\`
@@ -117,26 +117,6 @@ STRING: ( '"' ( '\\"' | '\\\\' | ~[\\"] )*? '"' )
117117
[[characters]]
118118
==== Characters
119119

120-
Use the <<painless-casting, casting operator>> to convert string literals or
121-
<<string-type, String>> values into <<primitive-types, char>> values.
122-
<<string-type, String>> values converted into
123-
<<primitive-types, char>> values must be exactly one character in length
124-
or an error will occur.
125-
126-
*Examples*
127-
128-
* Casting string literals into <<primitive-types, char>> values.
129-
+
130-
[source,Painless]
131-
----
132-
(char)"C"
133-
(char)'c'
134-
----
135-
+
136-
* Casting a <<string-type, String>> value into a <<primitive-types, char>> value.
137-
+
138-
[source,Painless]
139-
----
140-
String s = "s";
141-
char c = (char)s;
142-
----
120+
A character literal cannot be specified directly. Instead, use the
121+
<<string-character-casting, cast operator>> to convert a `String` type value
122+
into a `char` type value.

docs/painless/painless-operators.asciidoc

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ operator. See Function Calls [MARK] for more information.
240240
The brackets operator `[]` is used to create and access arrays, lists, and maps.
241241
The braces operator `{}` is used to intialize arrays.
242242

243+
[[array-initialization]]
243244
===== Creating and Initializing Arrays
244245

245246
You create and initialize arrays using the brackets `[]` and braces `{}`
@@ -248,9 +249,49 @@ initialize each dimension with are specified as a comma-separated list enclosed
248249
in braces. For example, `new int[] {1, 2, 3}` creates a one dimensional `int`
249250
array with a size of 3 and the values 1, 2, and 3.
250251

251-
For more information about allocating and initializing arrays, see <<array-type,
252-
Array Type>>.
252+
To allocate an array, you use the `new` keyword followed by the type and a
253+
set of brackets for each dimension. You can explicitly define the size of each dimension by specifying an expression within the brackets, or initialize each
254+
dimension with the desired number of values. The allocated size of each
255+
dimension is its permanent size.
253256

257+
To initialize an array, specify the values you want to initialize
258+
each dimension with as a comma-separated list of expressions enclosed in braces.
259+
For example, `new int[] {1, 2, 3}` creates a one-dimensional `int` array with a
260+
size of 3 and the values 1, 2, and 3.
261+
262+
When you initialize an array, the order of the expressions is maintained. Each expression used as part of the initialization is converted to the
263+
array's type. An error occurs if the types do not match.
264+
265+
*Grammar:*
266+
[source,ANTLR4]
267+
----
268+
declare_array: TYPE ('[' ']')+;
269+
270+
array_initialization: 'new' TYPE '[' ']' '{' expression (',' expression) '}'
271+
| 'new' TYPE '[' ']' '{' '}';
272+
----
273+
274+
*Examples:*
275+
[source,Java]
276+
----
277+
int[] x = new int[5]; // Declare int array x and assign it a newly
278+
// allocated int array with a size of 5
279+
def[][] y = new def[5][5]; // Declare the 2-dimensional def array y and
280+
// assign it a newly allocated 2-dimensional
281+
// array where both dimensions have a size of 5
282+
int[] x = new int[] {1, 2, 3}; // Declare int array x and set it to an int
283+
// array with values 1, 2, 3 and a size of 3
284+
int i = 1;
285+
long l = 2L;
286+
float f = 3.0F;
287+
double d = 4.0;
288+
String s = "5";
289+
def[] da = new def[] {i, l, f*d, s}; // Declare def array da and set it to
290+
// a def array with a size of 4 and the
291+
// values i, l, f*d, and s
292+
----
293+
294+
[[array-access]]
254295
===== Accessing Array Elements
255296

256297
Elements in an array are stored and accessed using the brackets `[]` operator.
@@ -298,6 +339,7 @@ return d[z]; // Access the 1st element of array d using the
298339
NOTE: The use of the `def` type in the second example means that the types
299340
cannot be resolved until runtime.
300341

342+
[[array-length]]
301343
===== Array Length
302344

303345
Arrays contain a special member known as 'length' that is a read-only value that contains the size of the array. This member can be accessed from an array using the dot operator.
@@ -727,6 +769,7 @@ def e; // declares the def variable e
727769
e = new HashMap(m); // sets e to a newly allocated HashMap using the constructor with a single argument m
728770
----
729771

772+
[[new-array]]
730773
==== New Array
731774

732775
An array type instance can be allocated using the new operator. The format starts with the new operator followed by the type followed by a series of opening and closing braces each containing an expression for the size of the dimension.

0 commit comments

Comments
 (0)