Cind language documentation

Classes

To create own object in object-oriented programming language (including the Cind language) there must be defined a schema of object behaviour, named class (the name comes from the general assumption, that there will be many objects of one class).

In Cind language, a class is a definition of behaviour of own-created objects, that contains definition of each object's context, catched methods, inheritance connection and other attributes.

Objects of defined classes are used as a data in Cind language, next to the other values (see Data types) and therefore, they can be stored in a variables as values (see Variables).

There are three kinds of classes: basic class (described here), monitor class (see Monitor class) and selector class (see Selector class).

Typical class definition looks like:

class c { int x = 0; .next() { return ++x; } }

Instruction new creates object of given class.
For example:

y = new c();

will create one object of the above class c and write it to the variable y.
An empty vector () given after the class name is a vector of parameters passed to the constructor of the object (see below).

For every created new object of defined class, a separate set of variables defined in the class will be reserved in memory.
This set of variables and other own attributes is named object's context (see below).

For more about instruction new see Creating objects.

A class can also refer to the other class, in so-called inheritance relation,
in the sense, that it can be some kind of a cover of the other class.
For inheritance relation see Class inheritance.

Type of object

Type of object of class is the class itself.
For example:

a.b.c x;

is a declaration of variable x of type a.b.c,
what means, that it will store objects of class a.b.c.
And this statement:

a.b.c x = new a.b.c();

declares variable x of type a.b.c and also creates one object of class a.b.c as a initial value of variable x.

For more about types see Data types.

Global and local classes

Classes can be divided into global and local.
Global class is declared out of any context, has name like a.b.c consisting space name and the class name, and objects of global class can be created everywhere.
Local class is declared in some context (inside some code), has name without dots, and objects of that class can be created only from the code, where class declaration is visible.

For more about space included in the class name see Spaces.

Context

Object's context is a set of variables, functions, conditions and other attributes defined in the class, which are created separately for each object of the class. This variables can be accessed only from the class' code.
Example:

class d { int x = 0; string s; ... }

In above, variables x and s belongs to the object's context of every object of class d.

An object's context is created for each object separately,
which distinguishes it from the static context, which is shared by all objects of given class.
For information about static context see Static code.

Notice, that it is unable to access object's context from the outside of the class code.
To be able from the outside to set some values assigned to the object use properties (see Object's properties).

Methods

A method of a class is a kind of function defined inside the class and executed in the object's context, but called from the outside. Method is identified by name. Declaration of method starts with dot character ( . ) before the method name, then there is a vector of parameters and then the code to execute.
For example:

class e { .sum(x,y) { return x+y; } ... }

is a definition of method named sum inside definition of the class e.
For syntax of parameters of method see Parameters of function.

To call a method combine an object, method name after dot, and call parameters.
For example:

var z = new e(); z.sum(a,b);

it creates object z of the above class e and then executes method sum of the object z with parameters (a,b).

A method of given object can be handled like a function, and stored in variable of fun type (see Functions). For example:

fun w = z.sum; w(x,y)

It is allowed to use a number of names in one method definition, which will be equivalent to defining a set of methods with the same code. The metod names must be separated by a vertical line character ( | ).
For example:

class k { . f | g | h (x,y) { ... } }

it defines a method with three names: f, g and h. Calling one of those methods will execute the same code.

General message catcher

General message catcher defined in the class allows to catch any message which has no corresponding catch method defined in the class.
Example:

class k { .f() { ... } .g|h() { ... } .[n]() { // general message catcher host.println("Got unexpected message: "+n); } }

Syntax of general message catcher at the beginning contains a dot character ( . ), then, instead of the method name, has a name of variable inside the [ ... ] brackets, then vector of parameters and of course a code to execute at the end.
The variable in the brackets will contain a string name of catched message.

In above example, calling methods f, g and h will execute appropriate methods, because they are defined in class, but calling any other method will execute general message catcher.

When class doesn't have a general message catcher defined, then not catched message will be passed down in inheritance chain (see Sending messages to objects and Class inheritance).

Constructor

Constructor of object is a piece of code, executed only once, when object is created.
The main role of constructor is to initialize object's context (assign values to variables, and so on) before the object will be used.
Constructor's section starts with keyword init, without dot at the begining, next is vector of parameters, and then code in brackets { ... }.
The result returned by constructor is omitted.
Example of constructor declaration inside class:

class c { init() { host.println("Creating another object of class c!"); ... } ... }

Parameters passed to object's constructor are taken from the instuction new.
For example, instruction:

x = new c(p1,p2,p3);

creates object of class c with vector (p1,p2,p3) passed as a parameters to the object's constructor.
This instruction suspends until the object's constructor is done.
For syntax of parameters see Parameters of function.

In fact, object's constructor consists of all code written in class declaration,
including variable initialization, code sections and static sections,
and will be executed in order of apperance in the class declaration,
but the parameters will be passed only to the init section.
For example for class:

class c { int x = 1; init() { x = 2; } int y = 3; .... }

constructor of object of this class will execute code in sequence:

x = 1; x = 2; y = 3;

Destructor

Destructor of object is a piece of code executed when object is no longer used and is going to be removed from the memory.
It is executed by program's execution software while is making memory cleanup (see Memory management).
Destructors are executed concurrently to the main program.

Destructor's section starts with keyword remove, without dot at the begining, next is an empty vector of parameters, and then code in brackets { ... }.
The result returned by destructor is omitted.
Example of destructor declaration inside class:

class c { release() { host.println("Removing another object of class c!"); ... } ... }

It is not determined when destructor will be executed, or even if it will ever be
(in example, when unused object is still stored in some forgotten variable, then execution software thinks that it is still used),
so programs algorithms should not rely on destructors.

Operators

An arithmetic instruction is an expression consists of the sign of the operator and the arguments.
For example:

x + y

is an arithmetic instruction, where + is the operator with arguments x and y (see Operators).

An operator may also be applied to the objects of classes. It that case, it must be defined inside the class of the object of its first argument. In above example, in the class of the object x.

An operator definition in the class consists of the colon character written twice ( :: ), a characters of the operator, parameters and the operators code. The result returned from the code will be a result of the operation. For example:

class myint { int value; ... ::+(a) { return new myint(value + a); } ::--() { --value; return this; } }

is a class containing definition of two operators + and --.
And therefore, it is possible to make the operations + and -- on the objects of the above class myint. For example:

x = new myint(); .... y = x + 10; y--;

making those arithmetic instructions will execute an appropriate operators code defined in the class.

Unlike in method definition, it is not allowed to define several operators in one definition,
because character of the separator ( | ) is also a symbol of the operator.

It is allowed to define only known operators, that is:
! ++ -- ~ * / % + - << >> < <= > >= == != & | ^ && ||
but not the new ones, for example: +++.

Notice also, that minus operator is distinguished from unary minus operator, by the number of parameters in its definition.

Type conversion

A conversion of the object to the different data type may be defined in its class, just like the operator, after characters ( :: ), then there is a name of the type, and then the code.
When object of a class needs to be converted to given type, the executing software will execute an appropriate type conversion method. The value returned from the code will not be treated as a conversion result, but will be converted again, until it finally reach expected data type.

Example of type conversion metods defined in the class:

class myint { int value; ... ::int { return value; } ::string { return "myint("+value+")"; } ::int[] { return [value] }; ::a.b.c { return new a.b.c(value); } ::float { return value; } }

For more information about converting objects see Type conversion.

Properties

Properties of object can be initially set in the class declaration, and they will be assigned to the object while executing the object constructor.
Syntax of the property declaration in the class is the property name and the value after colon character ( : ).

Example of properties declated in the class:

class item { name: "triangle"; position: (0,10); color: 7; ... }

For more about properties see Object's properties.



Cind programming language 1.0.4

 

Advertisements