<?xml version="1.0" ?>
<?xml-stylesheet type="text/css" href="stylesheet.css" ?>

<!-- Copyright 2003 Steve Folta -->

<cleet-doc xmlns:xlink="http://www.w3.org/1999/xlink">

<title> Objects and Classes </title>

<header> Objects: Fields and Functions </header>

<p>
Everything is an object in Cleet, even primitive types like
<id>Int</id> and <id>Bool</id>.
</p>

<p>
Objects have <i>fields</i> and <i>functions</i>.  Which fields and
functions the object has is specified by the object's class.  Fields
are the data that the object knows about.  In other languages, fields
are often called "instance variables".
</p>

<p>
An object's fields are visible to anyone who knows about the object.  In
other words, all fields are "public".  This goes against Smalltalk's
philosophy, but if fields are private, a lot of wasted effort tends to
go into creating getter and setter functions.  If a field shouldn't be
touched by other objects, a comment to that effect should be sufficient
to prevent any reasonable programmer from doing so.
</p>

<p>
Functions are basically little programs that the object can run.  The
code for a function is called its <i>method</i>.   Function names can
be any arbitrary string, but most of the time they'll be normal
identifiers.  Functions are differentiated from each other not only by
their names, but also by their argument types.  In C++ terms, they are
overloaded.  So an object might have these three different functions
with the same name:  <id>create()</id>, <id>create(value: Int)</id>,
and <id>create(name: String)</id>.
</p>

<p>
In fact, the <id>create</id> functions are somewhat special and are the
main reason Cleet has function overloading.  <id>create</id> functions
are called when the object is created and serve the same purpose as
constructors in C++.  Unlike C++ constructors, they are otherwise normal
functions and have no arcane rules about when virtual functions can and
can't be called.
</p>

<p>
Similarly, a class can have a <id>destroy()</id> function which is
called when an object goes away (that is, when the garbage collector
"finalizes" it).  This is needed only very rarely, mostly when some
system resource (such as a file handle) has been acquired and should be
released.
</p>

<header> Inheritance </header>

<p>
Cleet supports single inheritance.  (Multiple inheritance may be added
in the future.)  Any class for which a superclass hasn't been
specified is a subclass of <id>Object</id>.  All functions are virtual,
although compilers are encouraged to optimize function calls when they
can determine that virtual dispatch isn't required.  A class may have a
function without a method, in which case it is expected that subclasses
of the class will override it.  Such functions are called <i>phantom</i>
functions (the equivalent of C++'s "pure virtual" functions).
</p>

<header> Classes </header>

<p>
Classes are objects too.  The <id>class</id> function will return an
object's class.
</p>


<header> Class Fields and Functions </header>

<p>
A <i>class field</i> is a field that is shared by all instances of a
class (unlike normal fields, where each instance has its own).  A
<i>class function</i> is similar; it is part of the class, not the
instance, and so can only access class fields.
</p>

<p>
A common use for class fields and functions is the Singleton pattern. 
Here's an example:
</p>

<code>Class: DisplayScreen

the-display-screen: DisplayScreen;  [class]

screen: DisplayScreen
[class]
{
  if (the-display-screen == nil)
    the-display-screen = new DisplayScreen;
  return the-display-screen;
}</code>


<header> Class Files </header>

<p>
Currently, all code for a class is contained in a file, usually in a
single file.  The file has the same name as the class.  It consists of a
header followed by declarations of fields and functions.  For example:
</p>

<code>Class: HelloWorld
Superclass: Program


// fields:
message: String;


// functions:
create()
{
  message = "Hello world!\n";
}

run()
{
  message.write();
}</code>

<p>
A field is declared by following its name with a colon, its type, and a
semicolon.  Optionally, the semicolon can be followed by an attribute
(or attributes), as shown above in the "Class Fields and Functions"
example.
</p>

<p>
A function is declared by its name, its arguments (which are optional if
it has no arguments), its return type if it returns a value, its
attributes, and finally its code.  A "phantom" (ie. "virtual") function
will have the "phantom" attribute and will not have code:
</p>

<code>draw(display: Display)
[phantom]</code>

<p>
If you're declaring a function with a corresponding setter, you can add
the code for the setter as a code block immediately following the main
code block.  The extra argument will be named "new-value".  So this:
</p>

<code>name: String
{
  return this.the-name;
}
{
  this.the-name = new-value;
  redraw();
}</code>

<p>
is the same as this:
</p>

<code>name: String
{
  return this.the-name;
}

name-set(new-name: String)
{
  this.the-name = new-name;
  redraw();
}</code>


</cleet-doc>
