Variables

V

Assigning variables

The following code sets the variable counter to 5, and the variable username to “guest”. It then sets the repeated to the result of the call to the rep function (with the first two variables as function arguments), and passes that variable to the putStrLn function.

a = 5;
username = "guest";
repeated = rep(username,a);
putStrLn(repeated);

Operations on variables

Arithmetic operators

The usual set of arithmetic operators is available for floating point numbers and integers.

a = 5.2 + 2.8; // 8.0
b = 5.2 - 3.2; // 2.0
c = a * b; // 16.0
d = a / b; // 4.0
e = b ** 3.0; // 8.0 (b raised to the third power)

For integers only, the modulus operator is also available.

a = 6 % 4; // 2
b = -5 % 3; // -2

Arithmetic operators return a number of the same type as the operands. The following shorthand expressions are also available

a++; // a = a + 1;
a--; // a = a - 1;
a += 5; // a = a + 5;
a -= 5; // a = a - 5;

Unlike in other languages such as C, these shorthands all have type Void.

Comparison operators

The following boolean tests are available:

Operator Meaning Applies to
== equal to all types
!= not equal to all types
< less than numeric types
> greater than numeric types
<= less than or equal to numeric types
>= greater than or equal to numeric types
&& logical AND booleans only
|| logical OR booleans only

Logical AND and OR use short-circuit evaluation – if the left side gives enough information then the right side will not be evaluated. The following expression is true if b is between a and c.

((a < b && b < c) || (a > b && b > c))

Bitwise operators

When working with integers, the following bitwise operators are available:

a = 10 & 8; // 8 (bitwise AND)
b = 15 | 16; // 31 (bitwise OR)
c = 10 ^ 6; // 12 (bitwise XOR)
d = 6 << 2; // 24 (bitwise left shift)
e = 6 >> 2; // 1 (bitwise right shift)

String operators

When using strings, the + operator concatenates two strings together.

newstring = "abc" + "def"; // "abcdef"

Arrays

Kaya supports automatically-sized multi-dimensional arrays. You can set and get an array element using [] notation.

// 'a' is an array.
a[0] = 5;
a[1] = 10;
a[2] = a[0] + a[1]; // 15, of course.

You can also initialise an array by listing the initial contents inside square brackets.

a = [5,10,15];
b = ["foo","bar"];
c = []; // an empty array
d = [[10,20,30],[5,10],[1,2,3,4,5]]; // a 2-dimensional array
putStrLn(String(d[1][0])); // 5

Array indexes start at zero and can never be negative. All elements of an array must be of the same type. Accessing an uninitialised array value is allowed for arrays of integers (it will be initialised to zero), and causes a run-time error for all other arrays. It’s therefore generally best to initialise array elements before using them.

Array initialisation with ranges

Arrays of integers may be initialised with the range operator by specifying the first and last values. Ranges that skip values, or go down instead of up, can be initialised by also specifying the second value.

a = [1..5]; // a = [1,2,3,4,5]
b = [1,3..10]; // b = [1,3,5,7,9]
c = [10,9..1]; // c = [10,9,8,7,6,5,4,3,2,1]
d = [5,10..6]; // d = [5]

This is equivalent to calling the range function from the standard prelude.

Built-in (primitive) data types

All variables in Kaya are either of one of the primitive data types, or of a data type made up of some combination of primitive types. All types (primitive or otherwise) begin with a capital letter.

  • Int – 32-bit or 64-bit integers (depends on your hardware)
  • Char – Single unicode characters
  • Bool – Boolean (true/false)
  • Float – Double-precision floating point numbers
  • String – Strings of characters. The usual ‘\n’ for new line, ‘\t’ for tab, etc. escape characters can be used.
  • File – File handles
  • Ptr – Pointers to foreign language data
  • Exception – Exceptions
  • Void – The empty type

Everything contained within a function in Kaya has a type. For example, a call to the rand function has type Int. Assigning this to a variable with val = rand(); has type Void. The use of type inference means that when the type of a variable can be inferred from context, you do not need to explicitly state it. For example, the val variable is obviously of type Int and so there is no need to write Int val = rand(); (though you can if you want!). In general, you only need to explicitly give types for function return types and parameters.

Kaya contains no pointer types, except Ptr for foreign objects.

Any type t can be made into an array type using [t]. Arrays are automatically sized, and can be multi-dimensional ([[[Int]]] is a three-dimensional array of Ints, for example).

Complex data types

There are numerous data types in Kaya made up of the composition of primitive data types, or with ‘wildcard’ data types. The data types tutorial covers these in more detail.

Type inference

In general, it is not necessary to give the types of local variables as these can be inferred from context, although explicit type declarations can be given if desired. It is necessary to give the return type of functions and the types of function arguments.

Coercions

It is often useful to convert a value of one type into a value of another type. The syntax for this is Type(expression), for example String(30) or Int(getValue(data,10,20)). The following coercions are allowed:

  • Int can be converted to Char.
  • Int and String can be converted to Float.
  • CharFloat and String can be converted to Int.
  • BoolCharFloat and Int can be converted to String.

Where a binary operator (e.g. +) is used, both operands must have the same type. Kaya will automatically convert types if this is safe. For example, putStrLn("2 + 2 = "+(2+2)); will automatically be treated as putStrLn("2 + 2 = "+String(2+2)); because the conversion from Int to String is safe (i.e. cannot lose information). The following examples do not work, however:

  • putStrLn(5); – there is no binary operator.
  • putStrLn(5+5.72); – the Int can be converted to a Float to do the addition, but putStrLn needs a String.
  • pow(3,2+"5"); – a String cannot safely be converted to an Int.

In all of these cases, an explicit coercion must be used, for example pow(3,2+Int("5"));. Safe coercions are:

  • Int can be safely converted to Float.
  • Char and Bool can be safely converted to Int.
  • BoolCharFloat and Int can be safely converted to String.

Recent Comments

No comments to show.

Pages