Next: Object creation, Previous: Basics, Up: Syntax
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.
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.
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 null 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);
Kite also supports operator overloading using the operator
keyword. Overloadable
operators are below:
plus
minus
multiply
divide
mod
unplus
unminus
map
reduce
array
lt
gt
leq
geq
and
or
not
xor
lshift
rshift
call
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; ] ];
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.
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.