Next: , Previous: Basics, Up: Syntax


3.1.2 Declarations

3.1.2.1 Required Version

A module can specify the minimum required version in order to run. This can be done by using the version keyword, as shown below:

     version "1.2.0";
     "hello world"|print;

If a user attempts to run the above code on a version of Kite lower than 1.2.0, an exception will be thrown. The minimum supported version that can be specified here is 1.0.0. Only two of the version components are required–the third one will default to being 0 if not specified:

     version "1.2";
     "hello world"|print;
3.1.2.2 Classes

The class is the fundamental unit in Kite. All objects, whether built-in or user-created, are instances of classes. To declare a class, use the class keyword:

     class Hello from System.object [
        ...
     ];

The from System.object is optional, as all classes inherit from System.object by default. If specified, your class will inherit from the class specified. This allows for single inheritance:

     class Greetings [
         ...
     ];
     
     class Hello from Greetings [
         ...
     ];

All declarations inside of a class are delimited by commas, instead of semicolons.

3.1.2.3 Properties/variables

Properties and variables are the same in Kite. To declare, use the property keyword:

     class Greeting [
         property text,
         global property number_given
     ];
     
     property desired;

global is an optional keyword that goes before property that indicates that the property is to be shared amongst all instances of the class. This is equivalent to static members in C++ and similar languages.

Properties can also have a value assigned to them on first creation, as seen here:

     class Greeting [
         property text = "Hello!",
         global property number_given = 1
     ];
3.1.2.4 Methods

Methods work in a similar way as in other languages, and are declared using the method keyword:

     class Hello from Greeting [
         method sendGreeting(string) [
             string|print;
         ];
     ];
     
     method greetings() [
         (make Hello())|sendGreeting("hello");
     ];

Unlike in other languages, there is no concept of static vs. instance methods; however, this will be set to the class object inside of methods called without an instance and not called inside of a class. Methods can take any number of parameters, and method overloading is possible:

     class Hello from Greeting [
         method sendGreeting() [
             this|sendGreeting("hello");
         ],
         method sendGreeting(greeting) [
             greeting|print;
         ]
     ];

The return value of any method is the return value of the last executed statement in the method. In the example above, sendGreeting() will return whatever print returns.

Anonymous methods are also possible, by removing the method name from the declaration. Example:

     x = method(a) [
         (a + 1);
     ];
     (x(1))|print;

Any references to variables/properties inside a method call will search for the property down the call stack. That is, x inside the method will change the declaration outside the method unless a property declaration is included:

     property x;
     method y() [
         x = 3;
     ];
     
     x = 2;
     x|print;
     y();
     x|print;

To exit out of a method early, simply use the return keyword:

     method x(y)
     [
         return;
         "should not reach here"|print;
     ];
     x(1);
3.1.2.5 Operators

Kite also supports operator overloading using the operator keyword. Overloadable operators are below:

plus
Addition (+)
minus
Subtraction (-)
multiply
Multiplication (*)
divide
Division (/)
mod
Modulus (%)
unplus
Unary plus (+)
unminus
Unary minus (-)
map
Map (<-)
reduce
Reduce (<|)
array
Array dereference ([])
array_set
Array set ([]=)
lt
Less than (<)
gt
Greater than (>)
leq
Less than or equal to (<=)
geq
Greater than or equal to (>=)
and
Logical/bitwise AND (and)
or
Logical/bitwise OR (or)
not
Logical/bitwise NOT (not)
xor
Logical/bitwise XOR (xor)
lshift
Left shift (<<)
rshift
Right shift (>>)
call
Method call. Caution: the array passed in as the argument contains the method name for the first element (as a string) and a list containing the arguments as the second element. This operator is also called only if said method does not exist.

All operator methods above only take a single argument, the right hand side of the operation (if any). For the unary operators, this argument will be null. The left hand side is always this. Example:

     class Widget [
         property number,
         operator add(rhs) [
             property ret;
             ret = make Widget;
             ret.number = this.number + rhs.number;
             ret;
         ]
     ];
3.1.2.6 Constructors and Destructors

Constructors and destructors are created using the construct and destruct keywords:

     construct (param1, param2, ...) [
         ...
     ];
     destruct [
         ...
     ];

Constructors can also be overloaded, as appropriate. To call a base class's constructor, use base|__construct__(...) (base also works in any class method to call parent's version of said method). Any return value from the constructor is ignored.

3.1.2.7 Package importation

Packages are imported using the import keyword, with the name of the package in quotes:

     import "System.vm.thread";

Each component of the package is separated by a period and corresponds to an element of the path (see Extending Kite for more information).

Caution: import instructions are executed before any other code is run. This can interfere with proper operation if the package you're looking for is not in the standard Kite lookup path. Use eval (see Dynamic code evaluation) as a workaround.