2018-04-17 15:16:08 -04:00
|
|
|
[[painless-casting]]
|
|
|
|
=== Casting
|
|
|
|
|
2018-05-23 16:36:58 -04:00
|
|
|
A cast converts the value of an original type to the equivalent value of a
|
|
|
|
target type. An implicit cast infers the target type and automatically occurs
|
|
|
|
during certain <<painless-operators, operations>>. An explicit cast specifies
|
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
|
|
|
the target type and forcefully occurs as its own operation. Use the `cast
|
|
|
|
operator '()'` to specify an explicit cast.
|
|
|
|
|
|
|
|
Refer to the <<allowed-casts, cast table>> for a quick reference on all
|
|
|
|
allowed casts.
|
2018-04-17 15:16:08 -04:00
|
|
|
|
2018-05-23 16:36:58 -04:00
|
|
|
*Errors*
|
2018-04-17 15:16:08 -04:00
|
|
|
|
2018-05-23 16:36:58 -04:00
|
|
|
* If during a cast there exists no equivalent value for the target type.
|
|
|
|
* If an implicit cast is given, but an explicit cast is required.
|
2018-04-17 15:16:08 -04:00
|
|
|
|
2018-05-23 16:36:58 -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
|
|
|
|
2018-04-17 15:16:08 -04:00
|
|
|
[source,ANTLR4]
|
|
|
|
----
|
|
|
|
cast: '(' TYPE ')' expression
|
|
|
|
----
|
|
|
|
|
2018-05-23 16:36:58 -04:00
|
|
|
*Examples*
|
2018-04-17 15:16:08 -04:00
|
|
|
|
2018-05-23 16:36:58 -04:00
|
|
|
* Valid casts.
|
|
|
|
+
|
|
|
|
[source,Painless]
|
|
|
|
----
|
|
|
|
<1> int i = (int)5L;
|
|
|
|
<2> Map m = new HashMap();
|
|
|
|
<3> HashMap hm = (HashMap)m;
|
|
|
|
----
|
|
|
|
+
|
|
|
|
<1> declare `int i`;
|
|
|
|
explicit cast `long 5` to `int 5` -> `int 5`;
|
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 `i`
|
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-05-23 16:36:58 -04:00
|
|
|
<3> declare `HashMap hm`;
|
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 `m` -> `Map reference`;
|
2018-05-23 16:36:58 -04:00
|
|
|
explicit cast `Map reference` to `HashMap reference` -> `HashMap 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 `HashMap reference` to `hm`
|
2018-05-23 16:36:58 -04:00
|
|
|
|
|
|
|
[[numeric-type-casting]]
|
|
|
|
==== Numeric Type Casting
|
2018-04-17 15:16:08 -04:00
|
|
|
|
2018-05-23 16:36:58 -04:00
|
|
|
A <<primitive-types, numeric type>> cast converts the value of an original
|
|
|
|
numeric type to the equivalent value of a target numeric type. A cast between
|
|
|
|
two numeric type values results in data loss when the value of the original
|
|
|
|
numeric type is larger than the target numeric type can accommodate. A cast
|
|
|
|
between an integer type value and a floating point type value can result in
|
|
|
|
precision loss.
|
|
|
|
|
|
|
|
The allowed casts for values of each numeric type are shown as a row in the
|
|
|
|
following table:
|
2018-04-17 15:16:08 -04:00
|
|
|
|
|
|
|
|====
|
2018-05-23 16:36:58 -04:00
|
|
|
| | byte | short | char | int | long | float | double
|
|
|
|
| byte | | implicit | implicit | implicit | implicit | implicit | implicit
|
|
|
|
| short | explicit | | explicit | implicit | implicit | implicit | implicit
|
|
|
|
| char | explicit | explicit | | implicit | implicit | implicit | implicit
|
|
|
|
| int | explicit | explicit | explicit | | implicit | implicit | implicit
|
|
|
|
| long | explicit | explicit | explicit | explicit | | implicit | implicit
|
|
|
|
| float | explicit | explicit | explicit | explicit | explicit | | implicit
|
2018-04-17 15:16:08 -04:00
|
|
|
| double | explicit | explicit | explicit | explicit | explicit | explicit |
|
|
|
|
|====
|
|
|
|
|
2018-05-23 16:36:58 -04:00
|
|
|
*Examples*
|
|
|
|
|
|
|
|
* Valid numeric type casts.
|
|
|
|
+
|
|
|
|
[source,Painless]
|
|
|
|
----
|
|
|
|
<1> int a = 1;
|
|
|
|
<2> long b = a;
|
|
|
|
<3> short c = (short)b;
|
|
|
|
<4> double e = (double)a;
|
|
|
|
----
|
|
|
|
+
|
|
|
|
<1> declare `int a`;
|
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 1` to `a`
|
2018-05-23 16:36:58 -04:00
|
|
|
<2> declare `long b`;
|
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 `a` -> `int 1`;
|
2018-05-23 16:36:58 -04:00
|
|
|
implicit cast `int 1` to `long 1` -> `long 1`;
|
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 `long 1` to `b`
|
2018-05-23 16:36:58 -04:00
|
|
|
<3> declare `short c`;
|
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 `b` -> `long 1`;
|
2018-05-23 16:36:58 -04:00
|
|
|
explicit cast `long 1` to `short 1` -> `short 1`;
|
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 `short 1` value to `c`
|
2018-05-23 16:36:58 -04:00
|
|
|
<4> declare `double e`;
|
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 `a` -> `int 1`;
|
2018-05-23 16:36:58 -04:00
|
|
|
explicit cast `int 1` to `double 1.0`;
|
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 1.0` to `e`;
|
2018-05-23 16:36:58 -04:00
|
|
|
(note the explicit cast is extraneous since an implicit cast is valid)
|
|
|
|
+
|
|
|
|
* Invalid numeric type casts resulting in errors.
|
|
|
|
+
|
|
|
|
[source,Painless]
|
|
|
|
----
|
|
|
|
<1> int a = 1.0; // error
|
|
|
|
<2> int b = 2;
|
|
|
|
<3> byte c = b; // error
|
|
|
|
----
|
|
|
|
+
|
|
|
|
<1> declare `int i`;
|
|
|
|
*error* -> cannot implicit cast `double 1.0` to `int 1`;
|
|
|
|
(note an explicit cast is valid)
|
|
|
|
<2> declare `int b`;
|
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 2` to `b`
|
2018-05-23 16:36:58 -04:00
|
|
|
<3> declare byte `c`;
|
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 `b` -> `int 2`;
|
2018-05-23 16:36:58 -04:00
|
|
|
*error* -> cannot implicit cast `int 2` to `byte 2`;
|
|
|
|
(note an explicit cast is valid)
|
|
|
|
|
|
|
|
[[reference-type-casting]]
|
|
|
|
==== Reference Type Casting
|
|
|
|
|
|
|
|
A <<reference-types, reference type>> cast converts the value of an original
|
|
|
|
reference type to the equivalent value of a target reference type. An implicit
|
|
|
|
cast between two reference type values is allowed when the original reference
|
|
|
|
type is a descendant of the target type. An explicit cast between two reference
|
|
|
|
type values is allowed when the original type is a descendant of the target type
|
|
|
|
or the target type is a descendant of the original type.
|
|
|
|
|
|
|
|
*Examples*
|
|
|
|
|
|
|
|
* Valid reference type casts.
|
|
|
|
+
|
|
|
|
[source,Painless]
|
|
|
|
----
|
|
|
|
<1> List x;
|
|
|
|
<2> ArrayList y = new ArrayList();
|
|
|
|
<3> x = y;
|
|
|
|
<4> y = (ArrayList)x;
|
|
|
|
<5> x = (List)y;
|
|
|
|
----
|
|
|
|
+
|
|
|
|
<1> declare `List 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 value `null` to `x`
|
2018-05-23 16:36:58 -04:00
|
|
|
<2> declare `ArrayList y`;
|
|
|
|
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 `y`;
|
|
|
|
<3> load from `y` -> `ArrayList reference`;
|
2018-05-23 16:36:58 -04:00
|
|
|
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 `x`;
|
2018-05-23 16:36:58 -04:00
|
|
|
(note `ArrayList` is a descendant of `List`)
|
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
|
|
|
<4> load from `x` -> `List reference`;
|
2018-05-23 16:36:58 -04:00
|
|
|
explicit cast `List reference` to `ArrayList reference`
|
|
|
|
-> `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 `y`;
|
|
|
|
<5> load from `y` -> `ArrayList reference`;
|
2018-05-23 16:36:58 -04:00
|
|
|
explicit 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 `x`;
|
2018-05-23 16:36:58 -04:00
|
|
|
(note the explicit cast is extraneous, and an implicit cast is valid)
|
|
|
|
+
|
|
|
|
* Invalid reference type casts resulting in errors.
|
|
|
|
+
|
|
|
|
[source,Painless]
|
|
|
|
----
|
|
|
|
<1> List x = new ArrayList();
|
|
|
|
<2> ArrayList y = x; // error
|
|
|
|
<3> Map m = (Map)x; // error
|
|
|
|
----
|
|
|
|
+
|
|
|
|
<1> declare `List x`;
|
|
|
|
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 `x`
|
2018-05-23 16:36:58 -04:00
|
|
|
<2> declare `ArrayList 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
|
|
|
load from `x` -> `List reference`;
|
2018-05-23 16:36:58 -04:00
|
|
|
*error* -> cannot implicit cast `List reference` to `ArrayList reference`;
|
|
|
|
(note an explicit cast is valid since `ArrayList` is a descendant of `List`)
|
|
|
|
<3> declare `ArrayList 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
|
|
|
load from `x` -> `List reference`;
|
2018-05-23 16:36:58 -04:00
|
|
|
*error* -> cannot explicit cast `List reference` to `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
|
|
|
(note no cast is valid since neither `List` nor `Map` is a descendant of the
|
|
|
|
other)
|
2018-05-23 16:36:58 -04:00
|
|
|
|
|
|
|
[[dynamic-type-casting]]
|
|
|
|
==== Dynamic Type Casting
|
|
|
|
|
|
|
|
A <<dynamic-types, dynamic (`def`) type>> cast converts the value of an original
|
|
|
|
`def` type to the equivalent value of any target type or converts the value of
|
|
|
|
any original type to the equivalent value of a target `def` type.
|
|
|
|
|
|
|
|
An implicit cast from any original type value to a `def` type value is always
|
|
|
|
allowed. An explicit cast from any original type value to a `def` type value is
|
|
|
|
always allowed but never necessary.
|
|
|
|
|
|
|
|
An implicit or explicit cast from an original `def` type value to
|
|
|
|
any target type value is allowed if and only if the cast is normally allowed
|
|
|
|
based on the current type value the `def` type value represents.
|
|
|
|
|
|
|
|
*Examples*
|
2018-04-17 15:16:08 -04:00
|
|
|
|
2018-05-23 16:36:58 -04:00
|
|
|
* Valid dynamic type casts with any original type to a target `def` type.
|
|
|
|
+
|
|
|
|
[source,Painless]
|
2018-04-17 15:16:08 -04:00
|
|
|
----
|
2018-05-23 16:36:58 -04:00
|
|
|
<1> def d0 = 3;
|
|
|
|
<2> d0 = new ArrayList();
|
|
|
|
<3> Object o = new HashMap();
|
|
|
|
<4> def d1 = o;
|
|
|
|
<5> int i = d1.size();
|
|
|
|
----
|
|
|
|
+
|
|
|
|
<1> declare `def d0`;
|
|
|
|
implicit cast `int 3` to `def`;
|
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 3` to `d0`
|
2018-05-23 16:36:58 -04:00
|
|
|
<2> allocate `ArrayList` instance -> `ArrayList reference`;
|
|
|
|
implicit cast `ArrayList reference` to `def` -> `def`;
|
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 `def` to `d0`
|
2018-05-23 16:36:58 -04:00
|
|
|
<3> declare `Object o`;
|
|
|
|
allocate `HashMap` instance -> `HashMap reference`;
|
|
|
|
implicit cast `HashMap reference` to `Object reference`
|
|
|
|
-> `Object 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 `Object reference` to `o`
|
2018-05-23 16:36:58 -04:00
|
|
|
<4> declare `def d1`;
|
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 `o` -> `Object reference`;
|
2018-05-23 16:36:58 -04:00
|
|
|
implicit cast `Object reference` to `def` -> `def`;
|
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 `def` to `d1`
|
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
|
|
|
load from `d1` -> `def`;
|
2018-05-23 16:36:58 -04:00
|
|
|
implicit cast `def` to `HashMap reference` -> HashMap reference`;
|
|
|
|
call `size` on `HashMap reference` -> `int 0`;
|
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 0` to `i`;
|
2018-05-23 16:36:58 -04:00
|
|
|
(note `def` was implicit cast to `HashMap reference` since `HashMap` is the
|
|
|
|
child-most descendant type value that the `def` type value
|
|
|
|
represents)
|
|
|
|
+
|
|
|
|
* Valid dynamic type casts with an original `def` type to any target type.
|
|
|
|
+
|
|
|
|
[source,Painless]
|
|
|
|
----
|
|
|
|
<1> def d = 1.0;
|
|
|
|
<2> int i = (int)d;
|
|
|
|
<3> d = 1;
|
|
|
|
<4> float f = d;
|
|
|
|
<5> d = new ArrayList();
|
|
|
|
<6> List l = d;
|
|
|
|
----
|
|
|
|
+
|
|
|
|
<1> declare `def d`;
|
|
|
|
implicit cast `double 1.0` to `def` -> `def`;
|
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 `def` to `d`
|
2018-05-23 16:36:58 -04:00
|
|
|
<2> 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
|
|
|
load from `d` -> `def`;
|
2018-05-23 16:36:58 -04:00
|
|
|
implicit cast `def` to `double 1.0` -> `double 1.0`;
|
|
|
|
explicit cast `double 1.0` to `int 1` -> `int 1`;
|
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 1` to `i`;
|
|
|
|
(note the explicit cast is necessary since a `double` type value is not
|
|
|
|
converted to an `int` type value implicitly)
|
|
|
|
<3> store `int 1` to `d`;
|
2018-05-23 16:36:58 -04:00
|
|
|
(note the switch in the type `d` represents from `double` to `int`)
|
|
|
|
<4> declare `float 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
|
|
|
load from `d` -> `def`;
|
2018-05-23 16:36:58 -04:00
|
|
|
implicit cast `def` to `int 1` -> `int 1`;
|
|
|
|
implicit cast `int 1` to `float 1.0` -> `float 1.0`;
|
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 `float 1.0` to `f`
|
2018-05-23 16:36:58 -04:00
|
|
|
<5> 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 `d`;
|
2018-05-23 16:36:58 -04:00
|
|
|
(note the switch in the type `d` represents from `int` to `ArrayList`)
|
|
|
|
<6> declare `List l`;
|
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 `d` -> `def`;
|
2018-05-23 16:36:58 -04:00
|
|
|
implicit cast `def` to `ArrayList reference` -> `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
|
|
|
+
|
|
|
|
* Invalid dynamic type casts resulting in errors.
|
|
|
|
+
|
|
|
|
[source,Painless]
|
|
|
|
----
|
|
|
|
<1> def d = 1;
|
|
|
|
<2> short s = d; // error
|
|
|
|
<3> d = new HashMap();
|
|
|
|
<4> List l = d; // error
|
|
|
|
----
|
|
|
|
<1> declare `def d`;
|
|
|
|
implicit cast `int 1` to `def` -> `def`;
|
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 `def` to `d`
|
2018-05-23 16:36:58 -04:00
|
|
|
<2> declare `short s`;
|
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 `d` -> `def`;
|
2018-05-23 16:36:58 -04:00
|
|
|
implicit cast `def` to `int 1` -> `int 1`;
|
|
|
|
*error* -> cannot implicit cast `int 1` to `short 1`;
|
|
|
|
(note an explicit cast is valid)
|
|
|
|
<3> allocate `HashMap` instance -> `HashMap reference`;
|
|
|
|
implicit cast `HashMap reference` to `def` -> `def`;
|
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 `def` to `d`
|
2018-05-23 16:36:58 -04:00
|
|
|
<4> declare `List l`;
|
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 `d` -> `def`;
|
2018-05-23 16:36:58 -04:00
|
|
|
implicit cast `def` to `HashMap reference`;
|
|
|
|
*error* -> cannot implicit cast `HashMap reference` to `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
|
|
|
(note no cast is valid since neither `HashMap` nor `List` is a descendant of
|
|
|
|
the other)
|
2018-05-23 16:36:58 -04:00
|
|
|
|
|
|
|
[[string-character-casting]]
|
|
|
|
==== String to Character Casting
|
|
|
|
|
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 cast operator to convert a <<string-type, `String` type>> value into a
|
2018-05-23 16:36:58 -04:00
|
|
|
<<primitive-types, `char` type>> value.
|
|
|
|
|
|
|
|
*Errors*
|
|
|
|
|
|
|
|
* If the `String` type value isn't one character in length.
|
|
|
|
* If the `String` type value is `null`.
|
|
|
|
|
|
|
|
*Examples*
|
|
|
|
|
|
|
|
* Casting string literals into `char` type values.
|
|
|
|
+
|
|
|
|
[source,Painless]
|
|
|
|
----
|
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
|
|
|
<1> char c = (char)"C";
|
|
|
|
<2> c = (char)'c';
|
2018-05-23 16:36:58 -04:00
|
|
|
----
|
|
|
|
+
|
|
|
|
<1> declare `char c`;
|
|
|
|
explicit cast `String "C"` to `char C` -> `char C`;
|
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 `char C` to `c`
|
2018-05-23 16:36:58 -04:00
|
|
|
<2> explicit cast `String 'c'` to `char c` -> `char c`;
|
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 `char c` to `c`
|
2018-05-23 16:36:58 -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
|
|
|
* Casting a `String` reference into a `char` type value.
|
2018-05-23 16:36:58 -04:00
|
|
|
+
|
|
|
|
[source,Painless]
|
|
|
|
----
|
|
|
|
<1> String s = "s";
|
|
|
|
<2> char c = (char)s;
|
|
|
|
----
|
|
|
|
<1> declare `String s`;
|
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 `String "s"` to `s`;
|
2018-05-23 16:36:58 -04:00
|
|
|
<2> declare `char c`
|
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 `s` -> `String "s"`;
|
2018-05-23 16:36:58 -04:00
|
|
|
explicit cast `String "s"` to `char s` -> `char s`;
|
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 `char s` to `c`
|
2018-04-17 15:16:08 -04:00
|
|
|
|
|
|
|
[[boxing-unboxing]]
|
|
|
|
==== Boxing and Unboxing
|
|
|
|
|
2018-05-23 16:36:58 -04:00
|
|
|
Boxing is a special type of cast used to convert a primitive type to its
|
|
|
|
corresponding reference type. Unboxing is the reverse used to convert a
|
|
|
|
reference type to its corresponding primitive type.
|
|
|
|
|
|
|
|
Implicit boxing/unboxing occurs during the following operations:
|
|
|
|
|
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
|
|
|
* Conversions between a `def` type and a primitive type are implicitly
|
2018-05-23 16:36:58 -04:00
|
|
|
boxed/unboxed as necessary, though this is referred to as an implicit cast
|
|
|
|
throughout the documentation.
|
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
|
|
|
* Method/function call arguments are implicitly boxed/unboxed as necessary.
|
|
|
|
* A primitive type value is implicitly boxed when a reference type method
|
|
|
|
is called on it.
|
2018-04-17 15:16:08 -04:00
|
|
|
|
2018-05-23 16:36:58 -04:00
|
|
|
Explicit boxing/unboxing is not allowed. Use the reference type API to
|
|
|
|
explicitly convert a primitive type value to its respective reference type
|
|
|
|
value and vice versa.
|
2018-04-17 15:16:08 -04:00
|
|
|
|
2018-05-23 16:36:58 -04:00
|
|
|
*Errors*
|
2018-04-17 15:16:08 -04:00
|
|
|
|
2018-05-23 16:36:58 -04:00
|
|
|
* If an explicit cast is made to box/unbox a primitive type.
|
2018-04-17 15:16:08 -04:00
|
|
|
|
2018-05-23 16:36:58 -04:00
|
|
|
*Examples*
|
2018-04-17 15:16:08 -04:00
|
|
|
|
2018-05-23 16:36:58 -04:00
|
|
|
* Uses of implicit boxing/unboxing.
|
|
|
|
+
|
|
|
|
[source,Painless]
|
2018-04-17 15:16:08 -04:00
|
|
|
----
|
2018-05-23 16:36:58 -04:00
|
|
|
<1> List l = new ArrayList();
|
|
|
|
<2> l.add(1);
|
|
|
|
<3> Integer I = Integer.valueOf(0);
|
|
|
|
<4> int i = l.get(i);
|
2018-04-17 15:16:08 -04:00
|
|
|
----
|
2018-05-23 16:36:58 -04:00
|
|
|
+
|
|
|
|
<1> declare `List 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`;
|
|
|
|
<2> load from `l` -> `List reference`;
|
2018-05-23 16:36:58 -04:00
|
|
|
implicit cast `int 1` to `def` -> `def`;
|
|
|
|
call `add` on `List reference` with arguments (`def`);
|
|
|
|
(note internally `int 1` is boxed to `Integer 1` to store as a `def` type
|
|
|
|
value)
|
|
|
|
<3> declare `Integer I`;
|
|
|
|
call `valueOf` on `Integer` with arguments of (`int 0`) -> `Integer 0`;
|
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 `Integer 0` to `I`;
|
2018-05-23 16:36:58 -04:00
|
|
|
<4> 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
|
|
|
load from `I` -> `Integer 0`;
|
2018-05-23 16:36:58 -04:00
|
|
|
unbox `Integer 0` -> `int 0`;
|
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`;
|
2018-05-23 16:36:58 -04:00
|
|
|
call `get` on `List reference` with arguments (`int 0`) -> `def`;
|
|
|
|
implicit cast `def` to `int 1` -> `int 1`;
|
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 1` to `i`;
|
2018-05-23 16:36:58 -04:00
|
|
|
(note internally `int 1` is unboxed from `Integer 1` when loaded from a
|
|
|
|
`def` type value)
|
|
|
|
+
|
|
|
|
* Uses of invalid boxing/unboxing resulting in errors.
|
|
|
|
+
|
|
|
|
[source,Painless]
|
|
|
|
----
|
|
|
|
<1> Integer x = 1; // error
|
|
|
|
<2> Integer y = (Integer)1; // error
|
|
|
|
<3> int a = Integer.valueOf(1); // error
|
|
|
|
<4> int b = (int)Integer.valueOf(1); // error
|
|
|
|
----
|
|
|
|
+
|
|
|
|
<1> declare `Integer x`;
|
|
|
|
*error* -> cannot implicit box `int 1` to `Integer 1` during assignment
|
|
|
|
<2> declare `Integer y`;
|
|
|
|
*error* -> cannot explicit box `int 1` to `Integer 1` during assignment
|
|
|
|
<3> declare `int a`;
|
|
|
|
call `valueOf` on `Integer` with arguments of (`int 1`) -> `Integer 1`;
|
|
|
|
*error* -> cannot implicit unbox `Integer 1` to `int 1` during assignment
|
|
|
|
<4> declare `int a`;
|
|
|
|
call `valueOf` on `Integer` with arguments of (`int 1`) -> `Integer 1`;
|
|
|
|
*error* -> cannot explicit unbox `Integer 1` to `int 1` during assignment
|
2018-04-17 15:16:08 -04:00
|
|
|
|
|
|
|
[[promotion]]
|
|
|
|
==== Promotion
|
|
|
|
|
2018-05-23 16:36:58 -04:00
|
|
|
Promotion is when a single value is implicitly cast to a certain type or
|
|
|
|
multiple values are implicitly cast to the same type as required for evaluation
|
|
|
|
by certain operations. Each operation that requires promotion has a promotion
|
|
|
|
table that shows all required implicit casts based on the type(s) of value(s). A
|
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
|
|
|
value promoted to a `def` type at compile-time is promoted again at run-time
|
|
|
|
based on the type the `def` value represents.
|
2018-04-17 15:16:08 -04:00
|
|
|
|
2018-05-23 16:36:58 -04:00
|
|
|
*Errors*
|
2018-04-17 15:16:08 -04:00
|
|
|
|
2018-05-23 16:36:58 -04:00
|
|
|
* If a specific operation cannot find an allowed promotion type for the type(s)
|
|
|
|
of value(s) given.
|
2018-04-17 15:16:08 -04:00
|
|
|
|
2018-05-23 16:36:58 -04:00
|
|
|
*Examples*
|
|
|
|
|
|
|
|
* Uses of promotion.
|
|
|
|
+
|
|
|
|
[source,Painless]
|
|
|
|
----
|
|
|
|
<1> double d = 2 + 2.0;
|
|
|
|
<2> def x = 1;
|
|
|
|
<3> float f = x + 2.0F;
|
2018-04-17 15:16:08 -04:00
|
|
|
----
|
2018-05-23 16:36:58 -04:00
|
|
|
<1> declare `double 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
|
|
|
promote `int 2` and `double 2.0 @0`: result `double`;
|
2018-05-23 16:36:58 -04:00
|
|
|
implicit cast `int 2` to `double 2.0 @1` -> `double 2.0 @1`;
|
|
|
|
add `double 2.0 @1` and `double 2.0 @0` -> `double 4.0`;
|
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 4.0` to `d`
|
2018-05-23 16:36:58 -04:00
|
|
|
<2> declare `def x`;
|
|
|
|
implicit cast `int 1` to `def` -> `def`;
|
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 `def` to `x`;
|
2018-05-23 16:36:58 -04:00
|
|
|
<3> 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
|
|
|
load from `x` -> `def`;
|
2018-05-23 16:36:58 -04:00
|
|
|
implicit cast `def` to `int 1` -> `int 1`;
|
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
|
|
|
promote `int 1` and `float 2.0`: result `float`;
|
2018-05-23 16:36:58 -04:00
|
|
|
implicit cast `int 1` to `float 1.0` -> `float `1.0`;
|
|
|
|
add `float 1.0` and `float 2.0` -> `float 3.0`;
|
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 `float 3.0` to `f`;
|
2018-05-23 16:36:58 -04:00
|
|
|
(note this example illustrates promotion done at run-time as promotion
|
|
|
|
done at compile-time would have resolved to a `def` type value)
|
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
|
|
|
|
|
|
|
[[allowed-casts]]
|
|
|
|
==== Allowed Casts
|
|
|
|
|
|
|
|
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
|
|
|
|
indicates whether a cast to the specified target type is implicit (I), explicit
|
|
|
|
(E), or is not allowed (-).
|
|
|
|
|
|
|
|
*Primitive/Reference Types*
|
|
|
|
|
|
|
|
[cols="<3,^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
|
|
|
|
| boolean ( o ) | | - | - | - | - | - | - | - | - | - | - | - | - | - | - | - | - | - | I
|
|
|
|
| byte ( b ) | - | | I | I | I | I | I | I | - | - | - | - | - | - | - | - | - | - | I
|
|
|
|
| short ( s ) | - | E | | E | I | I | I | I | - | - | - | - | - | - | - | - | - | - | I
|
|
|
|
| char ( c ) | - | E | E | | I | I | I | I | - | - | - | - | - | - | - | - | E | - | I
|
|
|
|
| int ( i ) | - | E | E | E | | I | I | I | - | - | - | - | - | - | - | - | - | - | I
|
|
|
|
| long ( j ) | - | E | E | E | E | | I | I | - | - | - | - | - | - | - | - | - | - | I
|
|
|
|
| float ( f ) | - | E | E | E | E | E | | I | - | - | - | - | - | - | - | - | - | - | I
|
|
|
|
| double ( d ) | - | E | E | E | E | E | E | | - | - | - | - | - | - | - | - | - | - | I
|
|
|
|
| Boolean ( O ) | - | - | - | - | - | - | - | - | - | - | - | | - | - | - | - | - | - | I
|
|
|
|
| Byte ( B ) | - | - | - | - | - | - | - | - | - | | - | - | - | - | - | - | - | - | I
|
|
|
|
| Short ( S ) | - | - | - | - | - | - | - | - | - | - | | - | - | - | - | - | - | - | I
|
|
|
|
| Character ( C ) | - | - | - | - | - | - | - | - | - | - | - | | - | - | - | - | - | - | I
|
|
|
|
| Integer ( I ) | - | - | - | - | - | - | - | - | - | - | - | - | | - | - | - | - | - | I
|
|
|
|
| Long ( L ) | - | - | - | - | - | - | - | - | - | - | - | - | - | | - | - | - | - | I
|
|
|
|
| Float ( F ) | - | - | - | - | - | - | - | - | - | - | - | - | - | - | | - | - | - | I
|
|
|
|
| Double ( D ) | - | - | - | - | - | - | - | - | - | - | - | - | - | - | - | | - | - | I
|
|
|
|
| String ( T ) | - | - | - | E | - | - | - | - | - | - | - | - | - | - | - | - | | - | I
|
|
|
|
| Reference ( R ) | - | - | - | - | - | - | - | - | - | - | - | - | - | - | - | - | - | @ | I
|
|
|
|
|====
|
|
|
|
|
|
|
|
@ See <<reference-type-casting, reference type casting>> for allowed reference
|
|
|
|
type casts.
|
|
|
|
|
|
|
|
*`def` Type*
|
|
|
|
|
|
|
|
[cols="<3,^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
|
|
|
|
| def as boolean | I | - | - | - | - | - | - | - | I | - | - | - | - | - | - | - | - | - |
|
|
|
|
| def as byte | - | I | I | I | I | I | I | I | - | I | I | I | I | I | I | I | - | - |
|
|
|
|
| def as short | - | E | I | E | I | I | I | I | - | E | 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 int | - | 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 Boolean | I | - | - | - | - | - | - | - | I | - | - | - | | - | - | - | - | - |
|
|
|
|
| def as Byte | - | I | I | I | I | I | I | I | - | I | I | I | I | I | I | I | - | - |
|
|
|
|
| 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.
|