Basics

Guides

API Reference

Menu

Basics

Guides

API Reference

Classes

Aussom is class-based. Every line of code lives inside a class, and every value you can pass around is either a primitive or an instance of a class. This page covers constructors, inheritance, and the static modifier for singleton classes. The closely-related topic of calling Java code lives in the Extern Classes page.

Class declaration

A class declaration has a name and a body. Class names follow the identifier rules.

class Greeter {
    public name = "";

    public Greeter(string Name) {
        this.name = Name;
    }

    public greet() {
        return "Hello " + this.name;
    }
}

You instantiate it with the new operator:

g = new Greeter("Avery");
c.log(g.greet());

A file can contain more than one class. Group closely-related ones together; the runtime treats every class in the file as defined.

Constructors

A constructor is a function with the same name as the class. The runtime calls it once, automatically, after new allocates the instance.

class Account {
    public balance = 0;
    public owner = "";

    public Account(string Owner, int OpeningBalance = 0) {
        this.owner = Owner;
        this.balance = OpeningBalance;
    }
}

Calling code:

a = new Account("Avery");                // OpeningBalance defaults to 0
b = new Account("Robin", 100);

Constructors are not required. If you do not define one, new runs no setup beyond initializing the literal defaults you wrote in the class body.

Inheritance

Aussom supports single and multiple inheritance. Place a colon and a comma-separated list of parent class names after the class name.

class Animal {
    public run()   { c.log("running ..."); }
    public eat()   { c.log("eating ..."); }
    public sleep() { c.log("sleeping ..."); }
}

class Dog : Animal {
    public bark() { c.log("woof!"); }
    public wag()  { c.log("wagging tail"); }
}

class App {
    public main(args) {
        d = new Dog();
        d.run();        // inherited from Animal
        d.bark();       // defined in Dog
    }
}

Multiple inheritance

A class may inherit from more than one parent. Methods from every parent become available on the child.

class Carnivore {
    public hunt() { c.log("you don't see me ..."); }
}

class Dog : Animal, Carnivore {
    public bark() { c.log("woof!"); }
}
d = new Dog();
d.run();        // from Animal
d.hunt();       // from Carnivore
d.bark();       // from Dog

If two parents define a method with the same name, the one listed first wins. Avoid this when you can - it tends to surprise readers.

Inheriting from extern classes

A class may inherit from at most one extern class (a class that wraps Java code). You can mix one extern parent with any number of Aussom parents:

class MyView : SomeExternView, Logger, Configurable {
    // ...
}

Multiple extern parents are not supported.

Static classes

A static class is a singleton. The runtime instantiates it automatically when it is referenced, and you call its methods on the class name itself - no new required. Two examples you have already seen are c (the console) and sys.

static class app {
    public version = "1.0.0";

    public hello() {
        return "hello from app v" + this.version;
    }
}

class Main {
    public main(args) {
        c.log(app.hello());      // call directly on the class name
        c.log(app.version);
    }
}

Use static for utilities that have no per-instance state - things like math helpers, configuration accessors, or wrappers around process-global resources.

Comparing instances with instanceof

instanceof checks whether a value is an instance of a particular class. The right side is a string with the class name.

if (animal instanceof "Dog") {
    animal.bark();
}

This walks the inheritance chain, so a Dog is also instanceof "Animal".

What to read next

  • Extern Classes - declaring an Aussom view of a Java class so you can call into the JVM standard library or your own host code.
  • Modules - splitting your classes across files with include.