Predefined Types
String
stringdata type defines a sequence of characters. For example:"apple","red"are different of types of string values.stringvalues are always enclosed within double quotes"value".
def string color = "red";
def string fruit;
fruit = "apple";
Strings are case-sensitive. This means that two strings with same letters but different casing are considered different. For example: "Apple" and "apple" are two different strings.
- The details of functions applicable on
stringare present at String functions
Integer
intdata type is used to represent integral values. For example: 2, -7 etc.
def int numberOfFruits = 7;
def int daysInMonth;
daysInMonth = 31;
- The details of functions applicable on
intare present at Numeric functions
Floating-point numbers
realdata type is used to represent floating-point values. For example: 2.45, -34.78 etc.
def real cost = 19.99;
def real salary;
salary = 97500.5;
- The details of functions applicable on
realare present at Numeric functions
Boolean
booleandata type representstrueandfalsevalues.
def boolean isLoggedIn = true;
def boolean isOddNumber;
isOddNumber = false;
Date
Datetype represents date values.
def Date today = new Date();
def Date joiningDate;
joiningDate = createDate(2018,12,18);
Date type only contains year, month and day. For using hours, minute and seconds use Time type and to use both date and time use DateTime data type.
- The details of functions applicable on
Dateare present at Date / Time / Date-time functions
Time
Timetype represents time values.
def Date now = new Time();
def Date wakingTime;
wakingTime = createTime(6,30,0);
- The details of functions applicable on
Timeare present at Date / Time / Date-time functions
Date-time
DateTimetype represents Date-time values.
def DateTime now = new DateTime();
def DateTime partyAt;
partyAt = createDateTime(2023,3,21,20,15,0);
- The details of functions applicable on
DateTimeare present at Date / Time / Date-time functions
Q. How do I get current date in Plasma ?
A. To get the current date in Plasma, you can use the new Date() constructor:def Date today = new Date();
Q. How do I get current time in Plasma ?
A. To get the current time in Plasma, you can use the new Time() constructor:def Time currentTime = new Time();
Q. How do I get current date and time in Plasma ?
A. To get the current date and time in Plasma, you can use the new DateTime() constructor:def DateTime currentDateTime = new DateTime();
List
Listrepresents ordered homogenous data. It is a composite type which means that it requires an additional type to be specified completely.- For example, list of fruits will be a
Listofstring. This is written asList<string> - You can have list of lists also. For example,
List<List<int>>. In fact, you can go as deep as you need to!
def List<string> favouriteFruits = ["apple", "banana", "watermelon"];
def List<int> primeNumbersLessThan10;
primeNumbersLessThan10 = [2,3,5,7];
- In the above example, you saw one way of creating lists. There are a lot of ways of creating lists.
- Explicitly defining elements -
["apple", "banana", "watermelon"],[2,3,5,7]etc. - Range shorthand - You can define numeric ranges quickly via using different range operators:
[start...end]- Both the starting and ending numbers are included[start..<end]- Starting number is included but ending number is not included[start<..end]- Starting number is not included but ending number is included[start<.<end]- Both the starting and ending numbers are not included
- Explicitly defining elements -
def List<int> numbers = [1...5]; // This creates a list with elements 1, 2, 3, 4, 5
def List<int> numbers2 = [1..<10]; // This creates a list with elements 1, 2, 3, 4, 5, 6, 7, 8, 9
def List<int> numbers3 = [1<..10]; // This creates a list with elements 2, 3, 4, 5, 6, 7, 8, 9
def List<int> numbers4 = [1<.<10]; // This creates a list with elements 2, 3, 4, 5, 6, 7, 8, 9
- Using
range()functions - You can define numeric ranges with steps via functions. For example.range(2,9,3)will create[2,5,8] - Using functions - Create an empty
Listlikenew List<string>()and then use different functions to modify and manipulate the value. The details of functions applicable onListare present at List functions - Using
toList- You can convert from aSettoListby calling the functiontoListon it
Set
Setrepresents unordered, homogenous and unique data. It is a composite type which means that it requires an additional type to be specified completely.- For example, set of fruits will be a
Setofstring. This is written asSet<string>
def Set<string> favouriteFruits = createSetFrom("apple", "banana", "watermelon");
def Set<int> primeNumbersLessThan10;
primeNumbersLessThan10 = createSetFrom(2,3,5,7);
- In the above example, you saw one way of creating sets. There are a lot of ways of creating sets.
- Using
createSetFrom()function - Pass in the initial elements ofSetviacreateSetFrom(<Initial Elements>) - Using
toSet- You can convert from aListtoSetby calling the functiontoSeton it - Using constructor function - Create an empty
Setlikenew Set<string>()and then use different functions to modify and manipulate the value. The details of functions applicable onSetare present at Set functions
- Using
- Ordering -
Setcontains unordered data whileListcontains ordered data - Indexing - Since
Listis ordered we can access elements by indexing (eg:list[1]), while this feature is not available forSet - Uniqueness - Items in
Setare distinct from each other while no such feature is present inList
You can convert between List and Set very simply via:
toList()- Converts aSetto aListtoSet()- Converts aListto aSet
Map
Mapis used when you want to map one type of data to another. It is a composite type which means that it requires additional types to be specified completely.- For example, map of string to int will be a
Mapofstringtoint. This is written asMap<string, int>. The first type specifies of which type (calledKey) and the second type specifies to which type (calledValue). - This data can be particularly useful when you want to get data very quickly.
def Map<string, string> friendToInstrument = mapOf("Jimmy" to "guitar", "Robert" to "vocals", "Bonham" to "Drums");
def Map<string, real> employeeToSalary;
employeeToSalary = mapOf("John Doe" to 50000.0, "Jane Doe" to 75000.35);
-
In the above example, you saw one way of creating maps. There are a lot of ways of creating lists.
-
Use
mapOf- You can callmapOf()function with the desired values.TipThe syntax
"Jimmy" to "guitar"represents aMapEntrytype which will be explained below. -
Using functions - Create an empty
Maplikenew Map<string, int>()and then use different functions to modify and manipulate the value. The details of functions applicable onMapare present at Map functions
-
Map Entry
MapEntryrepresents an individual element of aMaptype. It is a composite type which means that it requires additional types to be specified completely.- Note that
MapEntryis available only in Plasma and cannot be used directly via UI in Askribe.
def MapEntry<string, string> guitarPlayer = "Jimmy" to "guitar";
def MapEntry<string, real> techLead;
techLead = "Jane Doe" to 75000.35;
- The details of functions applicable on
MapEntryare present at Map functions
Any
anyis a special type which can be used to represent any kind of data.- For example, if you want to represent a value which can be a
string,int,booleanor any other type, you can useanytype. - Remember that usage of
anytype, is essentially, bypassing type checking. So use it judiciously. - The details of functions applicable on
anyare present at Any functions
def any value = "Hello, World!";
def any value2 = 10;
def any value3 = true;
- When
anytype is involved in equality checks, the behaviour of==and!=is different.- Firstly, the value of
anytype will be casted to the type of the other operand. - If the cast is not possible, the result is
false. - If the cast is possible, the result is
trueif both values are equal.
- Firstly, the value of
def any data = "John Doe";
def string name = "John Doe";
def boolean result1 = data == name; // This will be true
def boolean result2 = name == data; // This will throw a compilation error! Any type can appear only on the LHS when comparing to a value of another type
def any data = "12";
def int name = 12;
def boolean result1 = data == name; // This will be true
def boolean result2 = name == data; // This will throw a compilation error! Any type can appear only on the LHS when comparing to a value of another type
- The behaviour of '==' is not limited to "==" and "!=" operators only. It extends to functions too.
- For example, consider the following code which uses
eqfunction:
def any data = "John Doe";
def string name = "John Doe";
def boolean result1 = data.eq(name); // This will be true
def boolean result2 = eq(data,name); // This will be true
def boolean result3 = name.eq(data); // This will throw a compilation error! Any type can appear only on the LHS when comparing to a value of another type
def boolean result4 = eq(name, data); // This will throw a compilation error! Any type can appear only on the LHS when comparing to a value of another type
- Let's take another example:
def List<any> data = ["John Doe", "1", true];
def boolean result1 = data.containsElement("1"); // This will be true
def boolean result2 = containsElement(data,"1"); // This will be true
def boolean result3 = data.containsElement(1); // This will be true
def boolean result4 = containsElement(data, 1); // This will be true
- Now, let's take one last example:
def List<any> list = ["apple", "1", true, 17.45];
def boolean value1 = list.containsAllElements(["apple", 1]); // true
def boolean value2 = containsAllElements(list, ["apple", 1]); // true
def boolean value2 = list.containsAllElements(["apple", false]); // false
def boolean value4 = containsAllElements(list, ["apple", false]); // false
def Set<string> set = createSetFrom("apple", "1", true, 17.45);
def boolean value5 = set.containsAllElements(createSetFrom("apple", 1)); // true
def boolean value6 = containsAllElements(set, createSetFrom("apple", 1)); // true
def boolean value7 = set.containsAllElements(createSetFrom("apple", false)); // false
def boolean value8 = containsAllElements(set, createSetFrom("apple", false)); // false
- The simple heuristic to remember is that all functions that perform equality checks in disguise can be used in the above manner.
- Obviously, all of this is applicable for not variants of the functions too
- List of such functions are:
FutureResult
FutureResultis a special type which can be used to represent a value which will be available in the future.- It is a composite type which means that it requires two additional types to be specified completely.
- The first type specifies the type of the data when the operation is successful (called
Result) - The second type specifies the type of the data when the operation is a failure (called
Error)
- The first type specifies the type of the data when the operation is successful (called
- For example, if you want to represent a value which returns a
stringon success and aninton failure, you can useFutureResult<string, int>type. - The details of functions applicable on
FutureResultare present at FutureResult functions - Note that this is an advanced type and is generally used when creating Plasma script templates
- Also note that
FutureResultis available only in Plasma and cannot be used directly via UI in Askribe.
def FutureResult<string, string> futureResult = new FutureResult<string, string>();
def FutureResult<string, int> futureResult2 = new FutureResult<string, int>();
def FutureResult<string, Employee[]> futureResult3 = new FutureResult<string, Employee[]>();
Conversion between types
- You can convert between types in Plasma.
- For example, you can convert between
stringandintusingtoString()andtoInt()functions. - Similarly, you can convert between
List<string>andSet<string>usingtoList()andtoSet()functions. - But how do you convert between between two arbitrary types?
- For example, how do you convert between an
Employeeclass and aContractorclass? - Or how do you convert from
anytype back to its original type?
- For example, how do you convert between an
- To achieve this, you can use Casting
Safe casting (istype operator)
- Safe casting is the process of first checking if the value can be converted to the desired type.
- If it can, then the value is converted to the desired type and assigned to a variable.
- If it cannot, then null is assigned
def any data = "John Doe";
if (data istype string name) {
log("data is a string with value: ", name);
} else if (data istype int age) {
log("data is an integer with value: ", age);
} else {
log("Unknown type");
}
- Note that the
assignmentpart of the if condition is optional. - If you do not want to assign the value to a variable, you can use the
istypeoperator directly.
def any data = "John Doe";
if (data istype string) {
log("Data is a string");
} else if (data istype int) {
log("Data is an integer");
} else {
log("Unknown type");
}
Dangerous casting ((NewType) expression operator)
- Dangerous casting is the process of converting a value to a desired type without any checks.
- If the value cannot be converted to the desired type, then an error is thrown.
- This is also known as unsafe casting.
- This is generally used when you are sure that the value can be converted to the desired type.
def any data = "John Doe";
def string name = (string) data;
def int age = (int) data; // This will throw an error
def any data2 = [1, 2, 3];
def List<int> numbers = (List<int>) data2;
def List<DateTime> dateTimes = (List<DateTime>) data2; // This will throw an error
Q. What is wrong with the following code ? def String name = "World";
A. There is no String type in Plasma. The correct type is string. So the corrected code is def string name = "World";
Q. What is wrong with the following code ? def Integer employeeId;
A. There is no Integer type in Plasma. The correct type is int. So the corrected code is def int employeeId;
Q. What is wrong with the following code ? def Decimal salary;
A. There is no Decimal type in Plasma. The correct type is real. So the corrected code is def real salary;
Q. What is wrong with the following code ? def Boolean isPermanent;
A. There is no Boolean type in Plasma. The correct type is boolean. So the corrected code is def boolean isPermanent;
Q. What is wrong with the following code ? def Timestamp orderDate
A. There is no Timestamp type in Plasma. The correct type is DateTime. So the corrected code is def DateTime orderDate
Q. What is wrong with the following code ? def MapEntry<string, string> guitarPlayer = new MapEntry<string,string>();
A. MapEntry does not have a constructor. You have to directly assign values to a MapEntry. For example: def MapEntry<string, string> guitarPlayer = "Jimmy" to "guitar";
Q. What is wrong with the following code ? def Date joiningDate = "2021-04-21";
A. You cannot directly assign a string to a Date type. You have to use the createDate function. So the corrected code is def Date joiningDate = createDate(2021,4,21);
Q. What is wrong with the following code ? def Time wakingTime = "06:30:00";
A. You cannot directly assign a string to a Time type. You have to use the createTime function. So the corrected code is def Time wakingTime = createTime(6,30,0);
Q. What is wrong with the following code ? def DateTime createdAt = "2021-04-21T06:30:00";
A. You cannot directly assign a string to a DateTime type. You have to use the createDateTime function. So the corrected code is def DateTime createdAt = createDateTime(2021,4,21,6,30,0);