Skip to main content

Predefined Types

String

  • string data type defines a sequence of characters. For example: "apple", "red" are different of types of string values. string values are always enclosed within double quotes "value".
    def string color = "red";
def string fruit;
fruit = "apple";
Tip

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.

Integer

  • int data type is used to represent integral values. For example: 2, -7 etc.
    def int numberOfFruits = 7;
def int daysInMonth;
daysInMonth = 31;

Floating-point numbers

  • real data 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;

Boolean

  • boolean data type represents true and false values.
    def boolean isLoggedIn = true;
def boolean isOddNumber;
isOddNumber = false;

Date

  • Date type represents date values.
    def Date today = new Date();
def Date joiningDate;
joiningDate = createDate(2018,12,18);
Tip

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.

Time

  • Time type represents time values.
    def Date now = new Time();
def Date wakingTime;
wakingTime = createTime(6,30,0);

Date-time

  • DateTime type represents Date-time values.
    def DateTime now = new DateTime();
def DateTime partyAt;
partyAt = createDateTime(2023,3,21,20,15,0);
Pro-tip

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

  • List represents 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 List of string. This is written as List<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
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 List like new List<string>() and then use different functions to modify and manipulate the value. The details of functions applicable on List are present at List functions
  • Using toList - You can convert from a Set to List by calling the function toList on it

Set

  • Set represents 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 Set of string. This is written as Set<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 of Set via createSetFrom(<Initial Elements>)
    • Using toSet - You can convert from a List to Set by calling the function toSet on it
    • Using constructor function - Create an empty Set like new Set<string>() and then use different functions to modify and manipulate the value. The details of functions applicable on Set are present at Set functions
Set vs List
  • Ordering - Set contains unordered data while List contains ordered data
  • Indexing - Since List is ordered we can access elements by indexing (eg: list[1]), while this feature is not available for Set
  • Uniqueness - Items in Set are distinct from each other while no such feature is present in List

You can convert between List and Set very simply via:

  • toList() - Converts a Set to a List
  • toSet() - Converts a List to a Set

Map

  • Map is 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 Map of string to int. This is written as Map<string, int>. The first type specifies of which type (called Key) and the second type specifies to which type (called Value).
  • 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 call mapOf() function with the desired values.

      Tip

      The syntax "Jimmy" to "guitar" represents a MapEntry type which will be explained below.

    • Using functions - Create an empty Map like new Map<string, int>() and then use different functions to modify and manipulate the value. The details of functions applicable on Map are present at Map functions

Map Entry

  • MapEntry represents an individual element of a Map type. It is a composite type which means that it requires additional types to be specified completely.
  • Note that MapEntry is 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 MapEntry are present at Map functions

Any

  • any is 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, boolean or any other type, you can use any type.
  • Remember that usage of any type, is essentially, bypassing type checking. So use it judiciously.
  • The details of functions applicable on any are present at Any functions
    def any value = "Hello, World!";
def any value2 = 10;
def any value3 = true;
Any type and equality checks
  • When any type is involved in equality checks, the behaviour of == and != is different.
    • Firstly, the value of any type 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 true if both values are equal.
    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 eq function:
    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

FutureResult

  • FutureResult is 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)
  • For example, if you want to represent a value which returns a string on success and an int on failure, you can use FutureResult<string, int> type.
  • The details of functions applicable on FutureResult are present at FutureResult functions
  • Note that this is an advanced type and is generally used when creating Plasma script templates
  • Also note that FutureResult is 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 string and int using toString() and toInt() functions.
  • Similarly, you can convert between List<string> and Set<string> using toList() and toSet() functions.
  • But how do you convert between between two arbitrary types?
    • For example, how do you convert between an Employee class and a Contractor class?
    • Or how do you convert from any type back to its original type?
  • 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 assignment part of the if condition is optional.
  • If you do not want to assign the value to a variable, you can use the istype operator 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

FAQ and Common Mistakes

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);