To use SIL like any other scripting language follow a few simple syntax rules.
General rules
- All instructions end with a semicolon ;
- Comments are introduced using //comment_until_end_of_line or /* comment */
Code Block |
---|
|
// single line comment
/*
this comment
spans over
multiple lines
*/ |
Types
Basic Types
SIL defines the following base types:
- string - defines a string, example literal is "abc"
- boolean - defines boolean values true and false
- number - any numeric value
- integer - a numeric value without a fractional component
- date - a date, like "2010-03-30"
- interval - a time interval, like "2d 3h"
Important notes
- interval - data type does not represent the working interval (i.e. 8h), but the full interval. When we created SIL™ we thought about if we should introduce a working interval type (i.e. 8h / day) but this would have certain undesired implications (how long is your workday? in France, for instance, Friday is only 6h). With our custom fields, it's a matter of interpretation of the interval, and we have routines to help you transform a 24h interval to a working interval, or you can simply write your own.
- date - data type can be used to represent dates and date-times.
- number - data type internal storage is represented only by real numbers (double). However, the language accepts for historical reasons type declarations as "integer", "real", "double", "float" (treating them as "number", of course).
Arrays
Multi-value (arrays) types are also supported and composed of the base type followed by the array symbols [ ] for each dimension of the array type. This translates to the following being valid array types:
- string[]
- boolean[]
- number[][]
- integer[]
- date[][][]
- interval[]
- Person[][] - where Person is a user-defined structure
Key-Value Arrays (Maps)
Arrays are actually maps where values are keyed by their position in the list, and so key-value arrays are declared the same as regular arrays.
Learn more about maps and the indexing operator.
Structures
The general syntax for defining a structure is:
Code Block |
---|
struct <name> {
<type> <fieldName>;
} |
Structures are defined by their name and pairs of field type and field name. A structure can have any number of fields. The type of each field can be a simple type, a user-defined structure (including the structure being defined) or arrays of these types.
Example:
Code Block |
---|
struct Person {
string id;
string name;
date birthDate;
Person manager;
Person [] subordinates;
} |
To access the value of a field from a variable the syntax will look like:
Code Block |
---|
<varName>.<fieldName> |
Initializing Structures:
Code Block |
---|
|
// considering structure Person defined above
Person p;
p.name = "John Doe";
p.id = "1234567";
string hisManager = p.manager.name;
string employee = p.subordinates[0].name; |
Code Block |
---|
|
struct P {
int x;
int y;
int z;
}
struct S {
string a;
number b;
date c;
P point;
};
S s = {"-text-", 3.14, "1974-03-15", {0, 1, 2}}; //one line init
//you do not have to expand the creation of the struct anymore
//you do not need to quote them as strings |
Constants
Info |
---|
This topic is about constant values, NOT about the read-only attribute of variables. |
Single Value Constants
For numbers and boolean values, constants are represented without any additional alterations.
Code Block |
---|
|
number n = 1;
number pi = 3.14;
boolean iLikeSIL = true; |
Constants for string, date and interval are provided between double quotes (").
- The full format for date is "yyyy-MM-ddTHH:mm:ss.SSSZ", and may include the time-zone information at the end. The short format "yyyy-MM-dd" is acceptedas well.
- The intervals should be provided in the JIRA standard format: "1w 2d 3h 4m".
Code Block |
---|
|
// valid dates
"2010-12-31"
"2010-12-31T24:59:59" (the default configured timezone is taken)
"2010-12-31T24:59:59Z" (Zulu time = UTC = GMT)
"2010-12-31T22:59:59+0200" (UTC+2)
"2010-12-31T24:59:59.999Z" (Zulu time = UTC = GMT)
"2010-12-31T22:59:59.999+0200" (UTC+2)
// interval
"1w 2d 3h 4m" |
Array Constants
Arrays are created using the following syntax:
Code Block |
---|
{<value1>, <value2>, ..., <value3>} |
Code Block |
---|
|
{"this", "is", "a", "string", "array"} // string array
{{0,1}, {2,3}, {4,5}} // number matrix
{true, false, false} // boolean array
{"1d", "2d", "3d"} // interval array
{ variable1, variable2, variable3 } // array of the type of variable#, where variable# can also be an array |
Note |
---|
|
We currently do not support constant representation of key-value arrays. You will need to build one using the indexing operator. |
Variable Declaration
The general syntax for declaring a variable is:
Code Block |
---|
<type> varname; // declaration without initialization
// or
<type> varname = <expression>; // declaration with initialization |
Code Block |
---|
|
string name = "John Doe";
integer random = 2;
number pi = 3.14;
boolean valid;
date today = currentDate();
interval spent = "1h 30m";
interval estimate = "2d" - spent;
number [][] matrix = {{0,1}, {2,3}, {4,5}}; |
Constant variables
Variables can be made read-only by adding the keyword "const" before the type when the variable is first defined.
Code Block |
---|
const string COMMA = ",";
const number PI = 3.14; |
If set to an array or structure, the read-only attribute will be applied to all the elements of the array or fields of the structure.
Routine Declaration
In addition to the routines provided out-of-the-box by SIL, you can define your local routines (user-defined routines or UDRs in short) that use the following syntax:
Code Block |
---|
function <name>(<type> param1, <type> param2, ...) {
Instruction1;
...
InstructionN;
return <value>;
} |
Learn more about UDRs.
Explicit Type Casts
Most of the time, SIL™ will attempt to do the type-casting for you automatically, however this cannot be always performed like this. These type casts allow you explicitly to convert a variable to a specific type.The general syntax for casting is:
Code Block |
---|
(<target_type>)varname |
Code Block |
---|
|
integer n = 1;
number x = 3.14;
string s = "2";
return n + s; // what will this return? 3 or "12"?
return (string)n + s; // now we know that adding 2 strings will result in "12"
return n + (number)s; // adding 2 numbers will definitely return a number (3) |
Note |
---|
|
Not all type casts are valid. For example it makes no sense to convert an interval to a date. |
See also