haXe

Packages and Imports

Each file can contain several classes, enums and imports. They are all part of the package declared at the beginning of the class. If package is not declared than the default empty package is used. Each type has then a path corresponding to the package name followed by the type name.

    // file my/pack/C.hx
    package my.pack;

    enum E {
    }

    class C {
    }

This file declares two types : my.pack.E and my.pack.C. It's possible to have several classes in the same file, but the type name must be unique in the whole application, so conflicts can appear if you're not using packages enough (this does not mean that you have to use long packages names everywhere).

When using packages, your files should be placed into subdirectories having the same name of it. In general the name of the file is the one of the main class defined into it.

The file extension for haXe is .hx.

Imports

Imports can be used to have access to all the types of a file without needing to specify the package name.

    package my.pack2;
    class C2 extends my.pack.C {
    }

Is identical to the following :

    package my.pack2;
    import my.pack.C;

    class C2 extends C {
    }

The only difference is that when using import you can use enum constructors that were declared in the my/pack/C.hx file.

Type Lookup

When a type name is found, the following lookup is performed, in this order:

  • current class type parameters
  • standard types
  • types declared in the current file
  • types declared in imported files (if the searched package is empty)
  • if not found, the corresponding file is loaded and the type is searched inside it

Enum Constructors

In order to use enum constructors, the file in which the enum is declared must first be imported, or you can use the full type path to access constructors as if they were static fields of the enum type.

    var c : my.pack.Color = my.pack.Color.red;

As an exception, in switch: if the type of the enum is known at compile time, then you can use constructors directly without the need to import.

«« Enums | Dynamic »»

version #1113, modified 2008-05-03 10:44:42 by ponticelli