Update casting table for Painless docs (#40491)

This updates the casting table to reflect the recent changes for casting consistency in Painless. This also adds a small section on explicitly casting a character to a String which has always been allowed but undocumented.
This commit is contained in:
Jack Conradson 2019-04-01 10:17:16 -07:00
parent 16a92689fc
commit 919358edec
1 changed files with 60 additions and 48 deletions

View File

@ -338,6 +338,28 @@ Use the cast operator to convert a <<string-type, `String` type>> value into a
explicit cast `String "s"` to `char s` -> `char s`; explicit cast `String "s"` to `char s` -> `char s`;
store `char s` to `c` store `char s` to `c`
[[character-string-casting]]
==== Character to String Casting
Use the cast operator to convert a <<primitive-types, `char` type>> value into a
<<string-type, `String` type>> value.
*Examples*
* Casting a `String` reference into a `char` type value.
+
[source,Painless]
----
<1> char c = 65;
<2> String s = (String)c;
----
<1> declare `char c`;
store `char 65` to `c`;
<2> declare `String s`
load from `c` -> `char A`;
explicit cast `char A` to `String "A"` -> `String "A"`;
store `String "A"` to `s`
[[boxing-unboxing]] [[boxing-unboxing]]
==== Boxing and Unboxing ==== Boxing and Unboxing
@ -464,61 +486,51 @@ based on the type the `def` value represents.
The following tables show all allowed casts. Read the tables row by row, where The following tables show all allowed casts. Read the tables row by row, where
the original type is shown in the first column, and each subsequent column the original type is shown in the first column, and each subsequent column
indicates whether a cast to the specified target type is implicit (I), explicit indicates whether a cast to the specified target type is implicit (I),
(E), or is not allowed (-). explicit (E), boxed/unboxed for methods only (A), a reference type cast (@),
or is not allowed (-). See <<reference-type-casting, reference type casting>>
for allowed reference type casts.
*Primitive/Reference Types* *Primitive/Reference Types*
[cols="<3,^1,^1,^1,^1,^1,^1,^1,^1,^1,^1,^1,^1,^1,^1,^1,^1,^1,^1,^1"] [cols="<3,^1,^1,^1,^1,^1,^1,^1,^1,^1,^1,^1,^1,^1,^1,^1,^1,^1,^1,^1,^1,^1"]
|==== |====
| | o | b | s | c | i | j | f | d | O | B | S | C | I | L | F | D | T | R | def | | O | N | T | b | y | s | c | i | j | f | d | B | Y | S | C | I | J | F | D | R | def
| boolean ( o ) | | - | - | - | - | - | - | - | - | - | - | - | - | - | - | - | - | - | I | Object ( O ) | | @ | @ | - | - | - | - | - | - | - | - | @ | @ | @ | @ | @ | @ | @ | @ | @ | I
| byte ( b ) | - | | I | I | I | I | I | I | - | - | - | - | - | - | - | - | - | - | I | Number ( N ) | I | | - | - | - | - | - | - | - | - | - | - | @ | @ | - | @ | @ | @ | @ | @ | I
| short ( s ) | - | E | | E | I | I | I | I | - | - | - | - | - | - | - | - | - | - | I | String ( T ) | I | - | | - | - | - | - | - | - | - | - | - | - | - | E | - | - | - | - | - | I
| char ( c ) | - | E | E | | I | I | I | I | - | - | - | - | - | - | - | - | E | - | I | boolean ( b ) | A | - | - | | - | - | - | - | - | - | - | A | - | - | - | - | - | - | - | - | I
| int ( i ) | - | E | E | E | | I | I | I | - | - | - | - | - | - | - | - | - | - | I | byte ( y ) | A | A | - | - | | I | E | I | I | I | I | - | A | A | - | A | A | A | A | - | I
| long ( j ) | - | E | E | E | E | | I | I | - | - | - | - | - | - | - | - | - | - | I | short ( s ) | A | A | - | - | E | | E | I | I | I | I | - | - | A | - | A | A | A | A | - | I
| float ( f ) | - | E | E | E | E | E | | I | - | - | - | - | - | - | - | - | - | - | I | char ( c ) | A | - | E | - | E | E | | I | I | I | I | - | - | - | A | A | A | A | A | - | I
| double ( d ) | - | E | E | E | E | E | E | | - | - | - | - | - | - | - | - | - | - | I | int ( i ) | A | A | - | - | E | E | E | | I | I | I | - | - | - | - | A | A | A | A | - | I
| Boolean ( O ) | - | - | - | - | - | - | - | - | - | - | - | | - | - | - | - | - | - | I | long ( j ) | A | A | - | - | E | E | E | E | | I | I | - | - | - | - | - | A | A | A | - | I
| Byte ( B ) | - | - | - | - | - | - | - | - | - | | - | - | - | - | - | - | - | - | I | float ( f ) | A | A | - | - | E | E | E | E | E | | I | - | - | - | - | - | - | A | A | - | I
| Short ( S ) | - | - | - | - | - | - | - | - | - | - | | - | - | - | - | - | - | - | I | double ( d ) | A | A | - | - | E | E | E | E | E | E | | - | - | - | - | - | - | - | A | - | I
| Character ( C ) | - | - | - | - | - | - | - | - | - | - | - | | - | - | - | - | - | - | I | Boolean ( B ) | A | - | - | A | - | - | - | - | - | - | - | | - | - | - | - | - | - | - | @ | I
| Integer ( I ) | - | - | - | - | - | - | - | - | - | - | - | - | | - | - | - | - | - | I | Byte ( Y ) | A | I | - | - | A | A | - | A | A | A | A | - | | A | - | A | A | A | A | @ | I
| Long ( L ) | - | - | - | - | - | - | - | - | - | - | - | - | - | | - | - | - | - | I | Short ( S ) | A | I | - | - | - | A | - | A | A | A | A | - | - | | - | A | A | A | A | @ | I
| Float ( F ) | - | - | - | - | - | - | - | - | - | - | - | - | - | - | | - | - | - | I | Character ( C ) | A | - | - | - | - | - | A | A | A | A | A | - | - | - | | A | A | A | A | @ | I
| Double ( D ) | - | - | - | - | - | - | - | - | - | - | - | - | - | - | - | | - | - | I | Integer ( I ) | A | - | - | - | - | - | - | A | A | A | A | - | - | - | - | | A | A | A | @ | I
| String ( T ) | - | - | - | E | - | - | - | - | - | - | - | - | - | - | - | - | | - | I | Long ( J ) | A | - | - | - | - | - | - | - | A | A | A | - | - | - | - | - | | A | A | @ | I
| Reference ( R ) | - | - | - | - | - | - | - | - | - | - | - | - | - | - | - | - | - | @ | I | Float ( F ) | A | - | - | - | - | - | - | - | - | A | A | - | - | - | - | - | - | | A | @ | I
| Double ( D ) | A | - | - | - | - | - | - | - | - | - | A | - | - | - | - | - | - | - | | @ | I
| Reference ( R ) | I | @ | @ | - | - | - | - | - | - | - | - | @ | @ | @ | @ | @ | @ | @ | @ | @ | I
|==== |====
@ See <<reference-type-casting, reference type casting>> for allowed reference
type casts.
*`def` Type* *`def` Type*
[cols="<3,^1,^1,^1,^1,^1,^1,^1,^1,^1,^1,^1,^1,^1,^1,^1,^1,^1,^1,^1"] [cols="<3,^1,^1,^1,^1,^1,^1,^1,^1,^1,^1,^1,^1,^1,^1,^1,^1,^1,^1,^1,^1"]
|==== |====
| | o | b | s | c | i | j | f | d | O | B | S | C | I | L | F | D | T | R | def | | O | N | T | b | y | s | c | i | j | f | d | B | Y | S | C | I | J | F | D | R
| def as boolean | I | - | - | - | - | - | - | - | I | - | - | - | - | - | - | - | - | - | | def as String | I | - | I | - | - | - | E | - | - | - | - | - | - | - | E | - | - | - | - | @
| def as byte | - | I | I | I | I | I | I | I | - | I | I | I | I | I | I | I | - | - | | def as boolean/Boolean | I | - | - | I | - | - | - | - | - | - | - | I | - | - | - | - | - | - | - | @
| def as short | - | E | I | E | I | I | I | I | - | E | I | E | I | I | I | I | - | - | | def as byte/Byte | I | - | - | - | I | I | E | I | I | I | I | - | I | I | E | I | I | I | I | @
| def as char | - | E | E | I | I | I | I | I | - | E | E | I | I | I | I | I | E | - | | def as short/Short | I | - | - | - | E | I | E | I | I | I | I | - | E | I | E | I | I | I | I | @
| def as int | - | E | E | E | I | I | I | I | - | E | E | E | I | I | I | I | - | - | | def as char/Character | I | - | - | - | E | E | I | I | I | I | I | - | E | E | I | I | I | I | I | @
| def as long | - | E | E | E | E | I | I | I | - | E | E | E | E | I | I | I | - | - | | def as int/Integer | I | - | - | - | E | E | E | I | I | I | I | - | E | E | E | I | I | I | I | @
| def as float | - | E | E | E | E | E | I | I | - | E | E | E | E | E | I | I | - | - | | def as long/Long | I | - | - | - | E | E | E | E | I | I | I | - | E | E | E | E | I | I | I | @
| def as double | - | E | E | E | E | E | E | I | - | E | E | E | E | E | E | I | - | - | | def as float/Float | I | - | - | - | E | E | E | E | E | I | I | - | E | E | E | E | E | I | I | @
| def as Boolean | I | - | - | - | - | - | - | - | I | - | - | - | | - | - | - | - | - | | def as double/Double | I | - | - | - | E | E | E | E | E | E | I | - | E | E | E | E | E | E | I | @
| def as Byte | - | I | I | I | I | I | I | I | - | I | I | I | I | I | I | I | - | - | | def as Reference | @ | @ | @ | - | - | - | - | - | - | - | - | @ | @ | @ | @ | @ | @ | @ | @ | @
| def as Short | - | E | I | E | I | I | I | I | - | E | I | E | I | I | I | I | - | - |
| def as Character | - | E | E | I | I | I | I | I | - | E | E | I | I | I | I | I | - | - |
| def as Integer | - | E | E | E | I | I | I | I | - | E | E | E | I | I | I | I | - | - |
| def as Long | - | E | E | E | E | I | I | I | - | E | E | E | E | I | I | I | - | - |
| def as Float | - | E | E | E | E | E | I | I | - | E | E | E | E | E | I | I | - | - |
| def as Double | - | E | E | E | E | E | E | I | - | E | E | E | E | E | E | I | - | - |
| def as String | - | - | - | E | - | - | - | - | - | - | - | - | - | - | - | - | I | - |
| def as Reference | - | - | - | - | - | - | - | - | - | - | - | - | - | - | - | - | - | @ |
|==== |====
@ See <<reference-type-casting, reference type casting>> for allowed reference
type casts.