# OTcl Autoloading (Version 0.96, Sept. 95)

## Overview

This reference page describes how arrange for OTcl classes and their methods to be demand loaded. The mechanism extends the existing Tcl autoloading scheme (with which you should be familiar) by using an `otcl_mkindex` procedure to add entries to a `tclIndex` file. It allows you to distribute classes across files without additional concern for inheritance dependencies, as well as distribute a class's methods across files.

## Extending a tclIndex

To add OTcl load entries to a `tclIndex`, use the `otcl_mkindex` procedure, defined during OTcl initialization. For example, to update the `tclIndex` in the current directory for the demand loading of classes, issue this shell command:

>     echo "otcl_mkindex Class . *.tcl" | otclsh

`otcl_mkindex` takes the same directory and filename pattern arguments as `auto_mkindex`, but also takes a list of classes as its first argument. This list, the creator list, describes the kind of objects that are candidates for demand loading. Usually this will be `Class`, as above, to cause index entries to be generated for classes. But it may also specify other classes if you are working with meta-classes or need loading for regular objects. `otcl_mkindex` appends the `tclIndex` file directly, and returns a string describing the number of object and method index entries it generated.

Like `auto_mkindex`, `otcl_mkindex` will only find obvious candidates for demand loading. Both explicit creation, via the `create` method, and implicit creation, via a method name that is not a known method of `Class`, are considered in the search. Objects (but not methods) must be created at the hard left margin, and object and method names may not begin with "$".

## How Loading Works

Autoloading of class and object commands is triggered via the regular Tcl `unknown` mechanism, so that invoking a missing class or object command will cause it to be loaded. In addition, OTcl demand loads in two other ways.

First, unknown classes referenced as superclasses will be demand loaded. This is triggered internally by the OTcl system. It means that you can distribute classes across files without concern for sourcing the files in inheritance order. It does not mean, however, that you can make forward references to classes within a file, or mutually recursive forward references across files.

Second, undefined methods may be demand loaded when they are invoked. This is arranged for autoloaded classes by the OTcl object loader, `otcl_load`. It means that you can distribute the definition of a class and its methods over more than one file.

Method level loading works as follows. When a class or object is being autoloaded by `otcl_load`, only those methods defined in the main class file that is sourced will be fully loaded. Other methods that are known to exist because of `auto_index` entries are then filled with load stubs by using the `auto` option of the `proc` and `instproc` methods. This guarantees that if they are invoked, they will be loaded. Note that these method load stubs must be installed, rather than relying on an unknown-style load scheme, to cater for the shadowing of methods. Also, you can control your own load policy by rewriting `otcl_load`.