Creation/Dev/GScript3: Difference between revisions

From Graal Bible
Line 53: Line 53:


Converts the source type of ''expression'' to ''target_type''.<br/>
Converts the source type of ''expression'' to ''target_type''.<br/>
The target type must be an object type, not a basic type like [[#int type|int]], '''number''', '''string'''.<br>
The target type must be an object type, not a basic type like [[#int type|int]], '''number''', [[#string type|string]].<br>
The source type must be a member of the target type.
The source type must be a member of the target type.



Revision as of 03:32, 6 June 2013

GScript3 is the latest version of the Graal's scripting language.
It introduces new syntax and semantic elements, and enable new features in Web browsers.

Language Elements

Operators

addition (+) operator

Usage

numeric_expression1 + numeric_expression2

Adds together numeric_expression1 and numeric_expression2.
Both expressions must be numeric (int or number types).

To concatenate strings, the append (@) operator must be used instead.

Example

echo(1 + 2); // 3
echo(1.5 + 2.25); // 3.75

addition assignment (+=) operator

Usage

numeric_expression1 += numeric_expression2

Puts the result of numeric_expression1 + numeric_expression2 into numeric_expression1.
Both expressions must be numeric (int or number types).

This is a direct equivalent of the following expression:

numeric_expression1 = numeric_expression1 + numeric_expression2

To concatenate strings, the append assignment (@=) operator must be used instead.

Example

var result : int = 10;
result += 20;
echo(result); // 30

append (@) operator

Usage

string_expression1 @ string_expression2

Concats together string_expression1 and string_expression2.
Both expressions must be string (string type).

To add numbers, the addition (+) operator must be used instead.

Example

echo("hello " @ "world"); // "hello world"

as operator

Usage

expression as target_type

Converts the source type of expression to target_type.
The target type must be an object type, not a basic type like int, number, string.
The source type must be a member of the target type.

Example

class Animal { ... }
class Cat extends Animal { ... }
class Dog extends Animal { ... }
var cat : Cat = new Cat();
var dog : Dog = new Dog();
var cat_as_animal : Animal =  cat as Animal; // OK as the Cat type is a member of the Animal type.
var cat_as_cat : Cat = cat_as_animal as Cat; // OK as the Cat type is a member of the Animal type.
var cat_as_dog : Dog = cat as Dog; // KO as the Cat type is not a member of the Dog type.
var cat_as_animal_as_dog : Dog = cat_as_animal as Dog; // null as the Cat as Animal type is not a member of the Dog type.

type (:) operator

Usage

var variable_name : type
function function_name(parameter : type, ...) : return_type { ... }

Specifies the type for a variable or function declaration.
For functions, this operator must be used to specify the return type, and the type of parameters if any.

Example

var language : string = "GScript";
var version : int = 3;

function sayHello() : void {
   echo("hello");
}

function addNumbers(a : int, b : int) : int {
   return a + b;
}

Types

int type

Enables to define variables, function parameters or return values with a 32-bit signed integer number type.

With this type, you can define integer numbers between -2,147,483,648 (-2 ^ 31) and 2,147,483,647 (2 ^ 31 - 1).
To work with floating-point numbers, or define numbers outside this range, the number type must be used instead.

Example

var integer_number : int = 123;
echo(123); // 123

string type

Represents textual data using a string of characters.
A string cannot be null but may be empty (with a length of zero).

Example

var text : string = "hello";
echo(text); // "hello"

void type

Usage

function function_name( ... ) : void { ... }

Specifies that a function does not return any value.
Only empty return statements must be used in functions with void type.

Example

function emptyFunction() : void {
}

function logMessage(message : string, hasLog : boolean) : void {
   if (!hasLog)
      return; // No returned value.
   echo(message);
}