Data Model Used in Examples
The following subsections use examples based on this data model:
package app
class Issue {
related : List<Reference<Issue>>
title : Text
description : Text
reporter : Reference<Person>
owner : Reference<Person>
}
class Person {
firstName : String
lastName : String
rolesByContext : Map<List<Reference<Role>>>
}
class Role {
title : Text
description : Text
}
class Comment {
issue : Reference<Issue>
author : Reference<Person>
publicationDate : DateTime
}
String Manipulation
| Operator |
Description |
Sample |
+ |
Use this operator to concatenate a string and a value. |
person.firstName + " is " + person.age + "
years-old"
|
{{...}}
|
Use this operator to substitute values in a string. |
You can write the example given for the +
operator above like this: {{person.firstName}} is
{{person.age}} years-old
|
Boolean Operation
| Operator |
Description |
Sample |
NOT |
Boolean NOT |
NOT foo IS NULL |
AND |
Boolean AND |
true AND false |
OR |
Boolean OR |
true OR false |
Conditional Expressions
| Operator |
Description |
Sample |
switch |
This operator is similar to the if else
operator. It is more readable than if else when you
have complex expressions. |
switch (p.age) {
case 20, 21 => "winner"
case 18, 19 => "looser"
default => "n/a"
}
|
|| |
This operator returns its right-hand side operand when its
left-hand side operand is null, and otherwise
returns its left-hand side operand. |
p.price || defaultPrice
|
if |
If else operator. Parentheses are mandatory but curly brackets
are optional. The else section is optional. |
if(p.age > 18) {
"adult"
} else {
"child"
}Example without curly
brackets:if (score >= 90) "A"
else if (score >= 80) "B"
else if (score >= 70) "C"
else "F" |
?: |
Ternary operator |
foo == "bar" ? 6 : 4
|
Comparison
| Operator |
Description |
Sample |
<= >= > < |
Relational operators |
p.age < 4 page.age IS Integer
|
== |
Equal operator |
|
!= |
Different operator |
|
IS |
Type check operator. Use this operator to test the type or the
nullity of an object. |
p IS Integerp IS
Item<social.Person>
p IS
Map<String, String>
p IS
Tuple<firstName:String, lastName:String,
age:Integer>
p IS
NULL
The IS operator does not
apply implicit casting, which means, for example, that 2
IS Float or 2 IS Any return
false. However, IS
supports the inheritance of items, which means that if the
Employee class extends
Person, employee IS
Item<social.Person> returns
true. |
Number Manipulation
| Operator |
Description |
Sample |
+ - |
As unary operators |
-2
|
* / % |
Multiply, divide |
p.age*4 p.age / 4 p.age % 4
|
+ - |
As binary operators |
p.age + 4
|
** |
Exponentiation operator. 1.5 ** 3 is identical
to Math.pow(1.5, 3).
Note:
You can also use
the function Integer.pow to avoid
floating-point rounding errors.
|
1.5 ** 3 |
Item Manipulation
| Operator |
Description |
Sample |
. |
Use this operator to access an attribute |
p.price
|
... |
Use this spread operator to expand an item into a tuple. It
retrieves all the item attributes to create a tuple out of
them. |
To convert an item in tuple and add it an attribute, you can
enter an expression like: {...item, fullName: item.firstName
+ " " + item.lastName}
|
-> |
Use the arrow operator to specify a traverse reference from index
unit items. Use -> to specify a direct traverse.
You can apply the arrow operator -> on
Reference<> or
List<Reference<>>.
This operator returns an Item or a
List<Item>, depending on the type of
attribute on which you apply it.
|
issue->owner.firstName
issue->owner->rolesByContext["editing"]
issue->related.first
|
<- |
The reverse arrow operator <- always returns
a List<Item>, as it is impossible to specify
the cardinality of the relation. |
issue<-[app.Comment.issue] or
issue<-[app.Comment] returns a
List<app.Comment> |
[] |
This dynamic access operator allows you to retrieve an attribute
value using a string expression while maintaining as much type
safety as possible through inference.
Use a cast operation if the
inference cannot guess the output type. For example, if you know
that the attribute to read is a string:
item[$attribute] AS
String;
NULL returns when trying to read a non-existent
attribute.
|
item["title"] is equivalent
to:(item AS Item<app.Issue>).title || (item AS Item<app.Role>).title |
List Manipulation
| Operator |
Description |
Sample |
. |
Use this operator to access an entry |
p.price
|
[]
|
Use this operator to access an entry in the list from its index. |
list[0]
|
IN |
This operator returns true if the specified
values are present in the list. |
p.age IN [4,5,6] returns true if
the value of p.age is present in the list
[4,5,6] |
... |
The spread operator expands a list into another list. You
typically use this operator to append an element to a list. |
The most simple use case is to append an element to a list.
[...list, 4].But you can implement more
complex cases, like:[...list1, 4,
...list2]. |
+ |
This operator allows you to merge 2 lists together, or adds an
element to a list. |
[1, 2, 3] + 4 returns [1, 2, 3,
4]
[1, 2] + [3, 4] returns [1, 2, 3,
4]
|
Map Manipulation
| Operator |
Description |
Sample |
... |
The spread operator expands a map into another map. It allows you
to add an element to the map.Notes:
If there are several
occurrences of the same key in the maps, it retains the value of
the last key. Keys cannot be NULL. The
expression language does not add NULL keys
to the map.
|
The most simple use case is to append a key/value pair to a map,
like: [...map,"foo":18]. |
[]
|
This operator retrieves a value in a map from its key. |
map["key"]
|
+ |
This operator allows you to merge 2 maps together. |
map + ["foo": 18]
|
.entries |
This operator returns the list of key/value pairs. |
|
Note:
You can also use several properties with the map type. For more information, see
Types.
Set Manipulation
| Operator |
Description |
Sample |
+ |
This operator allows you to merge 2 sets together, or adds an
element to a set. |
set + 18 adds value 18 to the set,
if it does not already exist. |
Tuple Manipulation
| Operator |
Description |
Sample |
... |
The spread operator expands a tuple into another tuple. It allows
you to add attributes to the tuple. Note:
If there are several
occurrences of the same attribute in the tuples, it retains the
value of the last attribute.
|
{...tuple, age: 40} adds the attribute
age with the value 40 to
the tuple.
You can also merge the attributes of several tuples together
using {...tuple1, ...tuple2}.
|
+ |
This operator allows you to merge 2 tuples together. Note:
If
there are several occurrences of the same attribute in the
tuples, it retains the value of the last attribute. For
example, {name: "Bob", age: 20} + {name: "Alice",
height: 180} returns {name: "Alice",
age: 20, height: 180}
|
{name: "Bob", age: 30} + {weight: 80, height:
180} returns a merged tuple with the attributes
name, age,
weight, and height. |
[] |
This operator allows you to retrieve a tuple attribute value
using a string expression while maintaining as much type safety
as possible through inference.
Use a cast operation if the
inference cannot guess the output type. For example, if you know
that the attribute to read is a string:
tuple[$attribute] AS
String;
NULL returns when trying to read a nonexistent
attribute.
|
tuple["title"] |
Cast Operation
| Operator |
Description |
Sample |
AS |
Use this operator to cast an element to a compatible
type. |
p AS DateTime
p.age AS Float
p AS
Item<social.Person>
p AS
Any
|
Bitwise Operation
| Operator |
Description |
Sample |
<< >> |
Bitwise shift operators |
p.age << 4
|
& |
Bitwise AND |
p.age & 4
|
^ |
Bitwise XOR |
p.age ^ 4
|
| |
Bitwise OR |
p.age | 4
|
Json Manipulation
jsonString = """
{
"person": {
"name": "John Smith",
"age": 32,
"isEmployed": true,
"hobbies": ["reading", "hiking", "photography"],
"address": {
"street": "123 Main St",
"city": "Anytown",
"zipcode": "12345"
}
}
}
""";
// Parsing JSON
json = JSON.parse(jsonString);
// Accessing simple properties
name = json["person"]["name"] AS String; // "John Smith"
age = json["person"]["age"] AS Integer; // 32
isEmployed = json["person"]["isEmployed"] AS Boolean; // true
// Type checking with 'IS'
json["person"]["name"] IS String; // true
json["person"]["age"] IS Integer; // true
json["person"]["age"] IS String; // false
// Default values
// Person has no 'height' property, so we'll get the default value
(json["person"]["height"] AS Integer) || 175; // 175
// Trying to convert string to integer, will get default
(json["person"]["name"] AS Integer) || 0; // 0
// Array handling
hobbies = json["person"]["hobbies"];
hobbies[0] AS String; // "reading"
hobbies.isArray(); // true
hoobies.arrayLength(); // 3
// Converting to List and using List functions
hobbiesList = hobbies.toList();
// Map all hobbies to uppercase
hobbiesList.map(hobby => (hobby AS String).toUpperCase()); // ["READING", "HIKING", "PHOTOGRAPHY"]
// Filter hobbies with more than 7 characters
hobbiesList.filter(hobby => (hobby AS String).length > 7); // ["photography"]
// Working with nested objects
address = json["person"]["address"];
address.isObject(); // true
// Get all field names from address
address.fieldNames(); // ["street", "city", "zipcode"]
// Check if a specific field exists
address.has("street"); // true
address.has("country"); // false
// Building a complete profile
{
fullName: json["person"]["name"] AS String,
age: json["person"]["age"] AS Integer,
city: json["person"]["address"]["city"] AS String,
hobbies: hobbiesList AS List<String>,
isRetired: ((json["person"]["age"] AS Integer) > 65) || false
};
|