painless-variables.asciidoc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. [[variables]]
  2. === Variables
  3. Variables in Painless must be declared and can be statically or <<dynamic-types,
  4. dynamically typed>>.
  5. [[variable-identifiers]]
  6. ==== Variable Identifiers
  7. Specify variable identifiers using the following grammar. Variable identifiers
  8. must start with a letter or underscore. You cannot use <<keywords, keywords>> or
  9. <<types, types>> as identifiers.
  10. *Grammar:*
  11. [source,ANTLR4]
  12. ----
  13. ID: [_a-zA-Z] [_a-zA-Z-0-9]*;
  14. ----
  15. *Examples:*
  16. [source,Java]
  17. ----
  18. _
  19. a
  20. Z
  21. id
  22. list
  23. list0
  24. MAP25
  25. _map25
  26. ----
  27. [[variable-declaration]]
  28. ==== Variable Declaration
  29. Variables must be declared before you use them. The format is `type-name
  30. identifier-name`. To declare multiple variables of the same type, specify a
  31. comma-separated list of identifier names. You can immediately assign a value to
  32. a variable when you declare it.
  33. *Grammar:*
  34. [source,ANTLR4]
  35. ----
  36. type: ID ('[' ']')*;
  37. declaration : type ID (',' ID)*;
  38. ----
  39. *Examples:*
  40. [source,Java]
  41. ----
  42. int x; // Declare a variable with type int and id x
  43. List y; // Declare a variable with type List and id y
  44. int x, y, z; // Declare variables with type int and ids x, y, and z
  45. def[] d; // Declare the variable d with type def[]
  46. int i = 10; // Declare the int variable i and set it to the int literal 10
  47. ----
  48. [[variable-assignment]]
  49. ==== Variable Assignment
  50. Use the equals operator (`=`) to assign a value to a variable. The format is
  51. `identifier-name = value`. Any value expression can be assigned to any variable
  52. as long as the types match or the expression's type can be implicitly cast to
  53. the variable's type. An error occurs if the types do not match.
  54. *Grammar:*
  55. [source,ANTLR4]
  56. ----
  57. assignment: ID '=' expression
  58. ----
  59. *Examples:*
  60. Assigning a literal of the appropriate type directly to a declared variable.
  61. [source,Java]
  62. ----
  63. int i;   // Declare an int i
  64. i = 10;  // Set the int i to the int literal 10
  65. ----
  66. Immediately assigning a value when declaring a variable.
  67. [source,Java]
  68. ----
  69. int i = 10; // Declare the int variable i and set it the int literal 1
  70. double j = 2.0; // Declare the double variable j and set it to the double
  71. // literal 2.0
  72. ----
  73. Assigning a variable of one primitive type to another variable of the same
  74. type.
  75. [source,Java]
  76. ----
  77. int i = 10; // Declare the int variable i and set it to the int literal 10
  78. int j = i;  // Declare the int variable j and set it to the int variable i
  79. ----
  80. Assigning a reference type to a new heap allocation with the `new` operator.
  81. [source,Java]
  82. ----
  83. ArrayList l = new ArrayList();  // Declare an ArrayList variable l and set it
  84. // to a newly allocated ArrayList
  85. Map m = new HashMap(); // Declare a Map variable m and set it
  86. // to a newly allocated HashMap
  87. ----
  88. Assigning a variable of one reference type to another variable of the same type.
  89. [source,Java]
  90. ----
  91. List l = new ArrayList(); // Declare List variable l and set it a newly
  92. // allocated ArrayList
  93. List k = l;  // Declare List variable k and set it to the
  94. // value of the List variable l
  95. List m;                   // Declare List variable m and set it the
  96. // default value null
  97. m = k;                    // Set the value of List variable m to the value
  98. // of List variable k
  99. ----