Autoloading

Demonstrates on-demand class loading; Go and Rust resolve everything statically at compile time and have no runtime autoloading.

C C23

C23
#include <stdio.h>
#include <string.h>

// C has no runtime class loading; a "registry + name prefix" emulates the autoloader's resolution timing:
// the type definition is resolved through the load table on first use
typedef const char *(*hello_fn)(void);

typedef struct {
    const char *class_name;
    hello_fn hello;
} AutoloadEntry;

// implementation provided after Greeter is "loaded"
static const char *greeter_hello(void) {
    return "Greeter has been autoloaded";
}

static const AutoloadEntry loaders[] = {
    { "Greeter", greeter_hello },
};

static const AutoloadEntry *autoload(const char *class_name) {
    for (size_t i = 0; i < sizeof(loaders) / sizeof(loaders[0]); i++) {
        if (strcmp(loaders[i].class_name, class_name) == 0) {
            return &loaders[i]; // hit: equivalent to the autoloader firing on first class use
        }
    }
    return NULL;
}

int main(void) {
    const AutoloadEntry *entry = autoload("Greeter");
    printf("%s", entry->hello());
    return 0;
}
Output
Greeter has been autoloaded

C++ C++26

C++26
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <string>
#include <utility>

// Greeter interface and implementation (in real projects usually in separate header files, "loaded" by a factory)
class Greeter {
public:
    virtual ~Greeter() = default;
    virtual std::string hello() const = 0;
};

class GreeterImpl : public Greeter {
public:
    std::string hello() const override {
        return "Greeter has been autoloaded";
    }
};

// static self-registering factory: emulates "type definition resolved on first use"
class Registry {
public:
    static Registry& instance() {
        static Registry r;
        return r;
    }
    void add(const std::string &name, std::function<std::unique_ptr<Greeter>()> maker) {
        makers_[name] = std::move(maker);
    }
    std::unique_ptr<Greeter> create(const std::string &name) const {
        return makers_.at(name)();
    }
private:
    std::map<std::string, std::function<std::unique_ptr<Greeter>()>> makers_;
};

// static registration object: registration completes before main runs (equivalent to registering the autoloader when the class definition loads)
static const bool kRegistered = [] {
    Registry::instance().add("Greeter", [] {
        return std::make_unique<GreeterImpl>();
    });
    return true;
}();

int main() {
    (void)kRegistered;
    auto g = Registry::instance().create("Greeter"); // first creation by name
    std::cout << g->hello();
    return 0;
}
Output
Greeter has been autoloaded

C# v11.0.100

v11.0.100
using System.Reflection;

// C# loads types via the CLR on first use; uses reflection (Assembly + Activator)
// to resolve and instantiate the type by name, emulating the autoloader's resolution timing
Assembly asm = Assembly.GetExecutingAssembly(); // equivalent of the autoload file: grab the already-registered assembly
Type t = asm.GetType("Greeter")!;               // resolve the type definition by name
object g = Activator.CreateInstance(t)!;        // instantiated only on first use
string result = (string)t.InvokeMember("Hello", BindingFlags.InvokeMethod, null, g, null)!;
Console.Write(result);

class Greeter
{
    public string Hello() => "Greeter has been autoloaded";
}
Output
Greeter has been autoloaded

Go v1.27.4

v1.27.4
package main

import (
    "fmt"
    "math"
)

// Go has no autoloading: imports are resolved statically by the toolchain at compile time,
// and unused imports are compile errors (dependency management via go.mod)
func main() {
    fmt.Println(math.Sqrt(16))
}
Output
4

Java v27.0.1

v27.0.1
import java.util.ArrayList;

public class Main {
    public static void main(String[] args) throws Exception {
        // the JVM loads classes from the classpath automatically on first use;
        // Class.forName can trigger loading explicitly (e.g. loading JDBC drivers)
        Class<?> c = Class.forName("java.util.ArrayList");
        Object list = c.getDeclaredConstructor().newInstance();
        System.out.println(c.getName());
        System.out.println(list instanceof ArrayList);
    }
}
Output
java.util.ArrayList
true

JavaScript v26.13.1

v26.13.1
// static imports are resolved and bundled by the bundler (Vite/webpack, etc.) at build time;
// dynamic import() loads on demand at runtime (a built-in module demonstrates the mechanism here)
import("node:os").then((m) => {
    console.log(typeof m.arch); // run the callback once on-demand loading completes
});
Output
function

PHP v8.6.0

v8.6.0
// register an autoload callback: fires when the class is first used
spl_autoload_register(function (string $class): void {
    // in real projects, PSR-4 maps namespaces to directory paths,
    // Composer generates the autoload files; the definition is given here directly to show the timing
    if ($class === "Greeter") {
        class Greeter {
            public function hello(): string {
                return "Greeter has been autoloaded";
            }
        }
    }
});

$g = new Greeter(); // only now is the autoloader triggered to load the class definition
echo $g->hello();
Output
Greeter has been autoloaded

Python v3.15.2

v3.15.2
import importlib

# Python's import is itself dynamic loading:
# import a module by name (may be a variable) and execute it; cached in sys.modules after the first import
m = importlib.import_module("math")
print(int(m.sqrt(16)))
print(m.pi)
Output
4
3.141592653589793

Rust v1.99.1

v1.99.1
// Rust has no autoloading: mod declarations are resolved at compile time,
// modules map to files by convention (mod greeter -> greeter.rs / greeter/mod.rs),
// everything is compiled into the final binary
mod greeter {
    pub fn hello() -> &'static str {
        "greeter module compiled into the binary"
    }
}

fn main() {
    println!("{}", greeter::hello());
}
Output
greeter module compiled into the binary

TypeScript v26.13.1

v26.13.1
// static imports are resolved by the compiler/bundler; dynamic import() loads on demand at runtime
// (a built-in module demonstrates the mechanism here)
const m = await import("node:os");
console.log(typeof m.arch); // continue after on-demand loading completes (top-level await)
Output
function

Language differences

C: no runtime class loading (symbols resolve at link time); a registry mimics the resolve-on-first-use timing.

C++: no runtime autoloading (everything resolves at compile/link time); the idiom is a static self-registering factory: register during static initialization, create by name on first use.

C#: the CLR loads a type on first use; Assembly reflection plus Activator.CreateInstance instantiates by name on demand, approximating an autoloader.

Go: all dependencies resolve and link statically at compile time, so runtime autoloading does not exist; dependency versions are managed with go.mod.

Java: the JVM loads classes from the classpath on first active use, and Class.forName triggers it explicitly (e.g. loading JDBC drivers).

JavaScript: static import is resolved by bundlers at build time; dynamic import() is available at runtime.

PHP: registers a callback with spl_autoload_register, triggered on first use of a class/interface/trait; real-world projects almost always use Composer, which generates autoload files following PSR-4 (namespace prefixes mapped to directories).

Python: import is itself runtime dynamic loading, and importlib.import_module can import by a string name.

Rust: all dependencies resolve and link statically at compile time, so runtime autoloading does not exist; modules are organized by file convention.

TypeScript: same as JS — static import plus dynamic import().