Skip to content

Namespace

Christophe Pollet edited this page Jul 8, 2019 · 16 revisions

Every class and type belongs to a namespace.

The namespace of a class or type is fully defined by the physical location on disk, i.e. the path of the file defining it, from the root namespace folder. This means that unlike Java, we don't have a package-like keyword to set the namespace of a type or class.

The root namespace folder is defined as the first folder containing an (empty) file named .throot, starting in the file's folder, going up the folder tree. This namespace's name is root but it's never used in practice, as nothing can live in it (i.e. there cannot be any type/class defined in a folder where a .throot file exists).

Given the following tree

/home/thlang/projects/example/
  .throot
  org/
    thoriumlang/
      example/
        Integer.th

The type or class defined in Integer.th belongs to namespace org.thoriumlang.example.

If a .throot file was present in /home/thlang/projects/example/org/thoriumlang, the namespace would have been example.

When depending on a type/class from another namespace, one need to explicitly declare it. Similarly to Java, we have a use keyword for that:

use org.thoriumlang.example.Integer;

Now, Integer is usable from the file. In case of name clashing between two types/classes from different namespaces, we can locally rename them as such:

use org.thiriumlang.example.Integer: RenamedInteger;

Now, we can use RenamedInteger for Integer. Using Integer is not possible.

One can import (and rename if needed) several types/classes from the same package like so:

use org.thoriumlang.example.{
  Integer: RenamedInteger,
  Float
};

One can import everything from a package with the * symbol:

use org.thoriumlang.example.*;

The following packages are imported automatically:

  • org.thoriumlang
  • root
  • current

Clone this wiki locally