Reflex Variables and Types - RapturePlatform/Rapture GitHub Wiki

Variables and Types

Variables in Reflex are created upon first declaration and can be named using the letters of the alphabet and the digits 0 - 9. Some examples of variable names are shown below:

Variable Name Valid
abc Yes
aB0 Yes
a*3 No (contains a *)
reflex Yes
_var No (contains a _)
println No (Reserved word)

Types in Reflex are inferred from the context in which a value is assigned, and wherever possible a type will be coerced into another if the other type is needed for a context. The types are listed below:

Type Example Description
string 'Hello' A string, an array of characters
number 4.0 A number, either an integer or a float, depending on context
boolean true A boolean value, either true or false
list [ 1, 2, 3] A list of values
map { 'key' : 'value' } An associate map, mapping keys (strings) to values.
date date() A date
time time() A time
file file('test.txt') A file object, used to read or write data.
nul null An object representing a nul value.
void void An object representing "no" object.
lib lib(className) An object representing a Reflex library.

Initialization

Reflex types are determined by context - for example the result of calling the built-in size function is a number, so assigning a variable to the result of calling that function will result in a numeric variable being created. Another way of defining the type of a variable is to initialize it. Reflex uses the format of the initialization to determine the type.

A variable initialization can also be prefixed with the keyword const. This keyword ensures that the value of this variable will be unchanged after first assignment, and that the variable will be accessible globally, including within functions.

String

The string type is initialized through a statement enclosed in either single quotes or double quotes. The double quote version allows you to perform simply substitution of variables within the string, as is seen in the examples below:

// String initialization
a = 'world';
b = 'hello';
c = 'hello, world';
d = "${a}, ${b}"; // equivalent to the value of c
const e = "There's a string in here somewhere";

Number

The number type is initialized through a statement that represents a number - either an integer (a series of digits) or a floating point number through either a series of digits, a decimal point and a further series of digits, or through the definition of a number through scientific notation. If you suffix a number representation with a capital L the number will be locked to an internal integer \index{integer} type, which is useful when using these numbers in native Java calls. If a number must be an integer post assignment you can use the cast function to convert it to an explicit integer.

// Number initializaion
a = 10;
b = 100.4;
const c = 5L;
d = 4.5E04; // equivalent to 45000

Boolean

The boolean type is defined using the keywords true and false, and of course can be initialized through the use of a boolean statement (see boolean operators in a later section).

// Boolean initialization
a = true;
const b = false;
c = (1 == 1); // c == true

List

The list type is defined using square brackets, with elements of the list being separated by commas. The elements can be any expression which includes the name of an existing (pre-defined) variable.

// List initialization
a = []; // An empty list
const b = [1, 2, 3, 4, 5]; 
c = ['A string', 1, []]; // A list of a string, a number, and an empty list

Map

A map is initialized using a JSON style format and is best demonstrated through example.

// Map initialization
a = {}; // An empty map
b = { 'a' : 4 }; // A map containing a 
                 // single entry, with 
                 // the key 'a' and the value 4
c = { 'one' : 1, 'two' : 2, 'three' : 3 };
const d = { 'outer' : { 'inner' : true }};

Simple Examples

Based on our understanding of types and variables, and our simple println function, we can create some new Reflex scripts. Here is a longer script that creates some variables and introduces some of the operators that will be expanded upon in the next section.

// An example Reflex script showing variables and types
x = 10;
y = "Hello";
z = x + 1;
println("Z is " + z);

In this example we have defined three variables, x, y and z. x and z are ultimately numbers and y is a string. We create the variable x on line 2, and y on line 3. z is implicitly created as the value of x+1 (i.e. 11). Finally we print out the value of z on line 5, but note that we have used the + operator on a string Z is) and a number (the variable z). In this case Reflex will convert z from a number to a string and then append the strings together.

Back to Overview