Proposal for virtual functionality

There are two types of virtual inheritance in this proposal, relaxed
(implicit) and strict (explicit). Relaxed is the simpler case that uses the
existing trait system with the addition of trait references and vtables.
Strict adds some constraints and requires some additional notation but allows
for down-casting.

Relaxed Virtual Inheritance:

Imagine the following code :

trait drawable(otype T) {
      void draw(T* );
};

struct text {
      char* text;
};

void draw(text*);

struct line{
      vec2 start;
      vec2 end;
};

void draw(line*);

While all the members of this simple UI support drawing, creating a UI that
easily supports both these UI requires some tedious boiler-plate code:

enum type_t { text, line };

struct widget {
      type_t type;
      union {
            text t;
            line l;
      };
};

void draw(widget* w) {
      switch(w->type) {
            case text : draw(&w->text); break;
            case line : draw(&w->line); break;
            default : handle_error(); break;
      }
}

While this code will work as implemented, adding any new widgets or any new
widget behaviors requires changing existing code to add the desired
functionality. To ease this maintenance effort required CFA introduces the
concept of trait references.

Using trait references to implement the above gives the following :

trait drawable objects[10];
fill_objects(objects);

while(running) {
      for(drawable object : objects) {
            draw(object);
      }
}

The keyword trait is optional (by the same rules as the struct keyword). This
is not currently supported in CFA and the lookup is not possible to implement
statically. Therefore we need to add a new feature to handle having dynamic
lookups like this.

What we really want to do is express the fact that calling draw() on a trait
reference should find the underlying type of the given parameter and find how
it implements the routine, as in the example with the enumeration and union.

For instance specifying that the drawable trait reference looks up the type
of the first argument to find the implementation would be :

trait drawable(otype T) {
      void draw(virtual T* );
};

This could be implied in simple cases like this one (single parameter on the
trait and single generic parameter on the function). In more complex cases it
would have to be explicitly given, or a strong convention would have to be
enforced (e.g. implementation of trait functions is always drawn from the
first polymorphic parameter).

Once a function in a trait has been marked as virtual it defines a new
function that takes in that trait's reference and then dynamically calls the
underlying type implementation. Hence a trait reference becomes a kind of
abstract type, cannot be directly instantiated but can still be used.

One of the limitations of this design is that it does not support double
dispatching, which concretely means traits cannot have routines with more than
one virtual parameter. The program must have a single table to look up the
function on. Using trait references with traits with more than one parameter
is also restricted, initially forbidden, see extension.

Extension: Multi-parameter Virtual Traits:

This implementation can be extended to traits with multiple parameters if
one is called out as being the virtual trait. For example :

trait iterator(otype T, dtype Item) {
	Maybe(Item) next(virtual T *);
}

iterator(int) generators[10];

Which creates a collection of iterators that produce integers, regardless of
how those iterators are implemented. This may require a note that this trait
is virtual on T and not Item, but noting it on the functions may be enough.


Strict Virtual Inheritance:

One powerful feature relaxed virtual does not support is the idea of down
casting. Once something has been converted into a trait reference there is
very little we can do to recover and of the type information, only the trait's
required function implementations are kept.

To allow down casting strict virtual requires that all traits and structures
involved be organized into a tree. Each trait or struct must have a unique
position on this tree (no multiple inheritance).

This is declared as follows :

trait error(otype T) virtual {
	const char * msg(T *);
}

trait io_error(otype T) virtual error {
	FILE * src(T *);
}

struct eof_error virtual io_error {
	FILE * fd;
};

So the trait error is the head of a new tree and io_error is a child of it.

Also the parent trait is implicitly part of the assertions of the children,
so all children implement the same operations as the parent. By the unique
path down the tree, we can also uniquely order them so that a prefix of a
child's vtable has the same format as its parent's.

This gives us an important extra feature, runtime checking of the parent-child
relationship with a C++ dynamic_cast like operation. Allowing checked
conversions from trait references to more particular references, which works
if the underlying type is, or is a child of, the new trait type.

Extension: Multiple Parents

Although each trait/struct must have a unique position on each tree, it could
have positions on multiple trees. All this requires is the ability to give
multiple parents, as here :

trait region(otype T) virtual drawable, collider;

The restriction being, the parents must come from different trees. This
object (and all of its children) can be cast to either tree. This is handled
by generating a separate vtable for each tree the structure is in.

Extension: Multi-parameter Strict Virtual

If a trait has multiple parameters then one must be called out to be the one
we generate separate vtables for, as in :

trait example(otype T, otype U) virtual(T) ...

This can generate a separate vtable for each U for which all the T+U
implementations are provided. These are then separate nodes in the tree (or
the root of different trees) as if each was created individually. Providing a
single unique instance of these nodes would be the most difficult aspect of
this extension, possibly intractable, though with sufficient hoisting and
link-once duplication it may be possible.

Example:

trait argument(otype T) virtual {
	char short_name(virtual T *);
	bool is_set(virtual T *);
};

trait value_argument(otype T, otype U) virtual(T) argument {
	U get_value(virtual T *);
};

Extension: Structural Inheritance

Currently traits must be the internal nodes and structs the leaf nodes.
Structs could be made internal nodes as well, in which case the child structs
would likely structurally inherit the fields of their parents.


Storing the Virtual Lookup Table (vtable):

We have so far been silent on how the vtable is created, stored and accessed.

Creation happens at compile time. Function pointers are found by using the
same best match rules as elsewhere (additional rules for defaults from the
parent may or may not be required). For strict virtual this must happen at the
global scope and forbidding static functions, to ensure that a single unique
vtable is created. Similarly, there may have to be stricter matching rules
for the functions that go into the vtable, possibly requiring an exact match.
Relaxed virtual could relax both restrictions, if we allow different vtable
at different conversion (struct to trait reference) sites. If it is allowed
local functions being bound to a vtable could cause issues when they go out
of scope, however this should follow the lifetime rules most C programs
already follow implicitly.

Most vtables should be stored statically, the only exception being some of
the relaxed vtables that could have local function pointers. These may be able
to be stack allocated. All vtables should be immutable and require no manual
cleanup.

Access has two main options:

The first is through the use of fat pointers, or a tuple of pointers. When the
object is converted to a trait reference, the pointers to its vtables are
stored along side it.

This allows for compatibility with existing structures (such as those imported
from C) and is the default storage method unless a different one is given.

The other is by inlining the vtable pointer as "intrusive vtables". This adds
a field to the structure to the vtable. The trait reference then has a single
pointer to this field, the vtable includes an offset to find the beginning of
the structure again.

This is used if you specify a vtable field in the structure. If given in the
trait the vtable pointer in the trait reference can then become a single
pointer to the vtable field and use that to recover the original object
pointer as well as retrieve all operations.

trait drawable(otype T) {
	vtable drawable;
};

struct line {
	vtable drawable;
	vec2 start;
	vec2 end;
};

This inline code allows trait references to be converted to plain pointers
(although they still must be called specially). The vtable field may just be
an opaque block of memory or it may allow user access to the vtable. If so
then there should be some way to retrieve the type of the vtable, which will be
autogenerated and often unique.


Keyword Usage:

It may be desirable to add fewer new keywords than discussed in this proposal.
It is possible that "virtual" could replace both "vtable" above with
unambiguous contextual meaning. However, for purposes of clarity in the design
discussion it is beneficial to keep the keywords for separate concepts distinct.


Trait References and Operations:

sizeof(drawable) will return the size of the trait object itself. However :

line a_line;
drawable widget = a_line;
sizeof(widget);

Will instead return the sizeof the underlying object, although the trait must
require that its implementation is sized for there to be a meaningful value
to return. You may also get the size of the trait reference with

sizeof(&widget);

Calling free on a trait reference will free the memory for the object. It will
leave the vtables alone, as those are (always?) statically allocated.
