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 file. If package is not declared then 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 creating types using packages, you should create a nested folder/directory structure matching the package name, and your files defining the type should be created in the innermost folder/directory. The name of the file should generally match the type you are creating. For example, to create the types E and C, in the package my.pack as shown above, your folder structure should be my\pack and the files could be E.hx and C.hx in the folder pack. In general, the name of the file is the one containing the definition of the main class.

The file extension for Haxe is .hx.

Each part of the path in package names must begin with a lower case letter and, like all types, type names in packages must begin with an upper case letter. Hence My.Pack is an invalid package, as is my.Pack. Similarly, my.pack.e would not be a valid type name or import.

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.

Applying additional static methods to class instances


The using keyword imports a file, additionally applying all static methods from that file as member functions on the type of their first parameter.

Say we have:

    package my.pack;
    class StringUtils {
        public static function double( string:String ):String {
            return string + string;
        }
    }

Then one could import the class and double a string:

    import my.pack.StringUtils;

    StringUtils.double( "Hello!"); // returns "Hello!Hello!"

Or, equivalently:

    using my.pack.StringUtils;

    "Hello!".double(); // returns "Hello!Hello!"

Importing a single type from a module


Given a module containing multiple types, such as
    package my.pack2;
    typedef A = { a : String }
    typedef B = { b : String }
it is possible to import one type at a time from that module, using the following syntax:
    import my.pack2.A;

Importing using wildcards ('*')

Importing multiple types with wildcards is NOT supported in Haxe 2, i.e.

    import flash.display.*;

will result in compiler error, quote "x.hx:1: characters x-x : Unexpected *".

The feature will however be supported in Haxe 3.

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 #15855, modified 2013-01-14 08:13:39 by jason