We see that externs can define both methods and variables (actually, PI is declared as a read-only property). Once this information is available to the compiler, it allows field access accordingly and also knows the types:

This works because the return type of method floor is declared to be Int.

The Haxe Standard Library comes with many externs for the Flash and JavaScript target. They allow accessing the native APIs in a type-safe manner and are instrumental for designing higher-level APIs. There are also externs for many popular native libraries on haxelib.

The Flash, Java and C# targets allow direct inclusion of native libraries from command line. Target-specific details are explained in the respective sections of Target Details.

Some targets such as Python or JavaScript may require generating additional "import" code that loads an extern class from a native module. Haxe provides ways to declare such dependencies also described in respective sections Target Details.

The haxe.extern package provides two types that help mapping native semantics to Haxe:

Rest<T>: This type can be used as a final function argument to allow passing an arbitrary number of additional call arguments. The type parameter can be used to constrain these arguments to a specific type.

EitherType<T1,T2>: This type allows using either of its parameter types, thus representing a type choice. It can be nested to allow more than two different types.

Externs support the private visibility modifier. However, because the default visibility in an extern class is public, private needs to be explicitly specified.

Specifying private members is helpful when an API intends to allow overriding functions. Also, Haxe cannot prevent subclasses from reusing field names unless if the fields are included in the extern definition. This is important on targets such as JavaScript where reusing a super class’s field name as a new field in a subclass is not supported.

externclassExampleSuperClass{privatefunctionnew(); // Require subclassing to use.// Only allow subclasses access to this overridable function.privatefunctionoverridableFunction():String;// This function is implicitly public:functiondoSomething():String;}