Cind language documentation

Variables

Variable is an abstract store location identified by name, which can hold some value.
Value stored in variable could be an object, numeric value, string, etc (see Data types).
Typical variable declaration:

int x = 10;

declares variable named x of int type with initial value 10. After declaration, variable x can be used in the code, by instructions of reading and writing, to store other values, but only of int type.

Variable can be associated with given block of code, with object, with thread, with function, or with the static region.

In fact, a variable is converted by the compiler to the specific place in memory, where holded value will be stored, or to specific register of the processor, or compiler combines it with other variables, or even removes it at all (when compiler found it useless or optimizes the code is such way, that code doesn't need that variable). It is better to think that variable is a reserved place in memory, after all.

Declaration

There are few ways to declare variable: with or without type, with initial value or not. It's even possible to use variable without prevoius declaration. Examples:

var x; // variable without type int y = 7; // variable with associated type 'int' and with initial value 7 a.b.c z; // variable z of objects of class a.b.c string[] w; // variable w is an array of strings var a,b; // two variables without type int c = 5, d; // two integer variables, first one have initial value [x,int y] = z; // declaration while restoring array t = ... ; // variable without declaration also can be used (see below)

Visibility

Every variable can be accessed in the block of its declaration below declaration
and also inside any other context, that are placed inside block of variable declaration and below declaration.

... // not visible (outside block { ... } with declaration) { ... // not visible (above declaration) var x; // variable declaration ... // visible (below declaration) { ... // visible (inside internal context) } } ... // not visible (outside block { ... } with declaration)

Value assignment and type mapping

If a variable has declared type, then each assignment to that variable will convert the assigned value to the given type.
For example this code:

int x; x = "123";

will convert string "123" to the integer value 123, and then will assign received value to the variable.

When type mapping is impossible, then there will be an exception fired from the place of assignment instruction (see Type conversion).

Before first assignation every variable has null value, which represents missed value (see Objects and null value).

Variables without declaration

It is possible to use variable without declaration.
A new variable will be defined by first assignation, for example:

y = 19;

it will define new variable named y, which can be used in code below definition as a regular variable but without assigned type.
The difference between declared and undeclared variable is, that undeclarated one cannot be accessed from upper contexts.

Constant variable

A constant variable cannot change assigned value.
It is defined by adding the prefix const before type in variable declaration.
Example:

const float pi = 3.14159265359;

Static variables

Static variable is assigned to the static context (see Static code).
It is defined by adding the prefix static before type in variable declaration.
Example:

static int x;

Variable preservation

Variable preservation is a technique, that allows access to the variable from the other contexts, and specially from the code, where variable is not visible.

To preserve variable, put dollar character ( $ ) before the variable name, in variable definition or in later instruction.
For example:

int $w = 4;

is a declaration of integer variable w and also a variable preservation, because of the $ character before the name.

Access to the preserved variable is provided by putting gravis character ( ` ) before the variable name,
For example:

x = `w; `z = y;

A preserved variable may still be used in the code as a regular variable.

For more information see Variable preservation.

Variables and threads

Any variable can be accessed concurrently by a number of threads.
Example code:

int x = 1; {# int z = x; x = 3; x += z; # x = 5; x++; # x = 7; x--; # host.println(x); }

Reading and writing to variable are the only atomic instructions, and they can be interleaved by the instructions from other concurrently working threads. To provide more security on variables, it is recommended to implement own synchronization mechanism for accessing them.
For more about concurrent access see Concurrent expressions.

Parameters as variables

Values passed to the function during function execution are catched as a values of the parameters. Parameters are treated like a local variables, that belongs to the context of function's code.

Declaration of the parameter is just like a declaration of variable, having given type and default value.
Examples:

fun(x, y) { ... } fun(x = a, int y) { ... } fun(int x, const string y = "abc") { ... } accept put(float[] $w) { ... }

For more about parameters see Parameters of function.



Cind programming language 1.0.4

 

Advertisements