Basics

Guides

API Reference

Menu

Basics

Guides

API Reference

Hello World

This page walks through the smallest useful Aussom program: one class, one function, one printed line.

The shape of a .aus file

Aussom is interpreted: the runtime reads a source file, parses it, and runs it. Every Aussom program lives in a file with a .aus extension, and every line of code lives inside a class. There is no top-level script-style code in Aussom.

To run a file from the command line:

$ aussom helloworld.aus

The interpreter looks for a class with a public main(args) function and starts there. args is a list of strings holding any extra arguments you passed on the command line.

Hello World

Open a text editor, save the following as helloworld.aus, and run it.

class HelloWorld {
    public main(args) {
        msg = "Hello World";
        c.log(msg);
    }
}

Output:

Hello World

What just happened, line by line:

  1. class HelloWorld { ... } declares a new class. The class is the container for everything the file does.
  2. public main(args) { ... } is the entry point the interpreter looks for. It must be public, and it must take a single args parameter.
  3. msg = "Hello World"; creates a local variable holding the string. Local variables are not declared with a type. They live only as long as the function is running.
  4. c.log(msg); prints to standard output. c is the built-in console class; its log method writes a line of text.

Adding a member, a constructor, and a helper

Real programs hold state, run setup code, and split work across methods. Below is the same idea, expanded:

class HelloWorld {
    // A member variable. Visible to every method on this object.
    public name = "";

    // The constructor. Runs once, when an instance is created.
    public HelloWorld() {
        this.setName("Avery");
    }

    public main(args) {
        // Create an instance. The constructor runs here.
        world = new HelloWorld();

        c.log("Hello " + world.name);
    }

    public setName(Name) {
        this.name = Name;
    }
}

Output:

Hello Avery

Three new ideas:

  • Member variable. public name = "" is a slot on every instance of HelloWorld. Inside the class you reach it through this.name. Outside the class, you reach it through the instance: world.name.
  • Constructor. A function with the same name as the class runs exactly once, right after new HelloWorld() allocates the object. Use it for setup that should always happen.
  • new operator. new HelloWorld() creates an instance. If the constructor takes arguments, you pass them here.

Why does the entry-point class need main?

When you run aussom helloworld.aus, the interpreter scans the file for the first class that defines a public main(args) function and starts there. A file is allowed to have other classes; only one of them needs main.

A common pattern is:

class Util {
    public greet(string Who) {
        return "Hello " + Who;
    }
}

class App {
    public main(args) {
        u = new Util();
        c.log(u.greet("Avery"));
    }
}

App.main is the entry point. Util is just a helper class in the same file.

What to read next

  • Variables - the difference between local and member variables, and access modifiers.
  • Data Types - every built-in type Aussom understands.