Cind programming language

Language definition Examples Download Contact

Type conversion

Methods for converting objects from one type to another one, are used by the system during execution of the program, and can be executed:

  • by putting object into a variable with assigned data type, or
  • by putting object in a designated place in language statement, or
  • by executing type cast operator.

Type conversion methods are integral parts of execution software, and mostly are internally implemented, but it is possible to implement type conversion methods for the objects of own class (see below).

Type conversion algorithm performs conversion iteratively, until it receives the object of the expected type.

For more about data types see Data types.

Conversion by assigning to variable

A variable can has assigned type in its declaration (see Variables), for example:

int x;

is a declaration of variable x with assigned type int, which means, that it can stores only values of integer type. Assigning any other objects to this variable will not trigger an error, but it will execute the corresponding method, to convert given object to the integer value, and then will store obtained value in the variable.

Examples of conversion by assigning:

int x = "234"; // x = 234 int x = 1.23f; // x = 1 char y = 0x0A; // y = eoln character (ascii value 0x0A) char y = "A"; // y = character 'A' (ascii value 0x41)

In case of variables from parameters of a fuction, the type conversion will be executed before the function execution.
For example, in this code:

fun inc(int x) { return x + 1; } string z = inc("2");

a string value "2" given in the parameter to the function inc will be converted to an integer value 2, because of the int type of parameter x, then function inc will return integer value 3, and finally variable z will store string value "3", because of the string type of this variable.

An operators also converts its parameters to expected type, before executing operation, for example:

"2" + 3 // will be "23" 2 + "3" // will be 5

Notice, that conversion of values of parameters will execute appropriate type conversion methods before executing the function code, but still inside given function, what means, that in example, in case of a monitor method, conversion of parameters will be executed inside a monitor lock grabed by that function, and in the case of accept instruction, conversion of parameters will not be executed after call, but after acceptance of call.

For more about parameters see Parameters of function.

Conversion by statement

Entering expressions in some designated places of language statements, will also executes appropriate type conversion method.
For example, in the statements:

if (x) { ... } {#[y] ... }

it will convert expression x to the bool type, because a boolean value is needed there, and convert expression y to the int type, to read the number of threads to execute.

Conversion by type cast operator

Type cast operator is an unary operator, consisting type in brackets (...) added to the left side of expression, which executes appropriate type conversion method.
For example:

x = (int)y;

converts value of the variable y to the int type and store the result in the variable x.

When name of the type may be interpreted by the compiler as something else, for example, when some variable also using the name, than expression of type cast operator (t)y may be interpreted as a function execution statement. To prevent this situation, there could be added a type word, before the type name. Example:

x = (type t)y;

It is also allowed to use typeof instruction inside type cast operator, for example:

x = (typeof(y))z;

For priority of type cast operator in expression see Priority in expression.
For typeof instruction see Operations on types.

Type conversion methods in classes

Type conversion methods can be defined inside class, allowing objects of given class to be converted to other types.

class c { ::int { return 1; } ::string { return "1" } ::bool { return true; } ::float { throw new exception("won't convert to float"); } ::a.b.c { return new a.b.c(); } ::int[][] { return [[1],[2,3]]; } }

above conversion methods, defined in the class c, will be executed when object of that class is going to be mapped to specified type.

Notice, that conversion process will always be performed iteratively, until the object of expected type will be received, so it is allowed to return from conversion method with objects of other type, knowing that it will be converted again.

The wanted type conversion method for an object of a class, will be searched in all inheritance chain.
For more about classes and inheritance chain see Classes and Class inheritance.

Cind programming language 1.0.4