Fetching contributors…
Cannot retrieve contributors at this time
1103 lines (802 sloc) 51.5 KB
\chapter{Standard Library}
\define[Type]{String}{define-string}{A String is a sequence of characters.}
%TODO: utf8 crap %
\paragraph{Character code}
Use the \ic{.code} property on a constant single-char string in order to compile its ASCII character code:
"#".code // will compile as 35
\paragraph{Related content}
\item See the \href{}{String API} for details about its methods.
\section{Data Structures}
An \type{Array} is a collection of elements. It has one \tref{type parameter}{type-system-type-parameters} which corresponds to the type of these elements. Arrays can be created in three ways:
\item By using their constructor: \expr{new Array()}
\item By using \tref{array declaration syntax}{expression-array-declaration}: \expr{[1, 2, 3]}
\item By using \tref{array comprehension}{lf-array-comprehension}: \expr{[for (i in 0...10) if (i \% 2 == 0) i]}
Arrays come with an \href{}{API} to cover most use-cases. Additionally they allow read and write \tref{array access}{expression-array-access}:
Since array access in Haxe is unbounded, i.e. it is guaranteed to not throw an exception, this requires further discussion:
\item If a read access is made on a non-existing index, a target-dependent value is returned.
\item If a write access is made with a positive index which is out of bounds, \expr{null} (or the \tref{default value}{define-default-value} for \tref{basic types}{types-basic-types} on \tref{static targets}{define-static-target}) is inserted at all positions between the last defined index and the newly written one.
\item If a write access is made with a negative index, the result is unspecified.
Arrays define an \tref{iterator}{lf-iterators} over their elements. This iteration is typically optimized by the compiler to use a \tref{\expr{while} loop}{expression-while} with array index:
Haxe generates this optimized \target{JavaScript} output:
Main.main = function() {
var scores = [110,170,35];
var sum = 0;
var _g = 0;
while(_g < scores.length) {
var score = scores[_g];
sum += score;
Haxe does not allow arrays of mixed types unless the parameter type is forced to \tref{\type{Dynamic}}{types-dynamic}:
\trivia{Dynamic Arrays}{In Haxe 2, mixed type array declarations were allowed. In Haxe 3, arrays can have mixed types only if they are explicitly declared as \expr{Array<Dynamic>}.}
\paragraph{Related content}
\item See the \href{}{Array API} for details about its methods.
\item \href{}{Data structures tutorials and examples} in the Haxe Code Cookbook.
A \type{Vector} is an optimized fixed-length \emph{collection} of elements. Much like \tref{Array}{std-Array}, it has one \tref{type parameter}{type-system-type-parameters} and all elements of a vector must be of the specified type, it can be \emph{iterated over} using a \tref{for loop}{expression-for} and accessed using \tref{array access syntax}{types-abstract-array-access}. However, unlike \type{Array} and \type{List}, vector length is specified on creation and cannot be changed later.
\type{haxe.ds.Vector} is implemented as an abstract type (\ref{types-abstract}) over a native array implementation for given target and can be faster for fixed-size collections, because the memory for storing its elements is pre-allocated.
\paragraph{Related content}
\item See the \href{}{Vector API} for details about the vector methods.
\item \href{}{Data structures tutorials and examples} in the Haxe Code Cookbook.
A \type{List} is a \emph{collection} for storing elements. On the surface, a list is similar to an \Fullref{std-Array}. However, the underlying implementation is very different. This results in several functional differences:
\item A list can not be indexed using square brackets, i.e. \expr{[0]}.
\item A list can not be initialized.
\item There are no list comprehensions.
\item A list can freely modify/add/remove elements while iterating over them.
A simple example for working with lists:
\paragraph{Related content}
\item See the \href{}{List API} for details about the list methods.
\item \href{}{Data structures tutorials and examples} in the Haxe Code Cookbook.
A \type{GenericStack}, like \type{Array} and \type{List} is a container for storing elements. It has one \tref{type parameter}{type-system-type-parameters} and all elements of the stack must be of the specified type. Here is a small example program for initializing and working with a \type{GenericStack}.
\trivia{FastList}{In Haxe 2, the GenericStack class was known as FastList. Since its behavior more closely resembled a typical stack, the name was changed for Haxe 3.}
The \emph{Generic} in \type{GenericStack} is literal. It is attributed with the \expr{:generic} metadata. Depending on the target, this can lead to improved performance on static targets. See \Fullref{type-system-generic} for more details.
\paragraph{Related content}
\item See the \href{}{GenericStack API} for details about its methods.
\item \href{}{Data structures tutorials and examples} in the Haxe Code Cookbook.
A \type{Map} is a container composed of \emph{key}, \emph{value} pairs. A \type{Map} is also commonly referred to as an associative array, dictionary, or symbol table. The following code gives a short example of working with maps:
Under the hood, a \type{Map} is an \tref{abstract}{types-abstract} type. At compile time, it gets converted to one of several specialized types depending on the \emph{key} type:
\item \type{String}: \type{haxe.ds.StringMap}
\item \type{Int}: \type{haxe.ds.IntMap}
\item \type{EnumValue}: \type{haxe.ds.EnumValueMap}
\item \type{\{\}}: \type{haxe.ds.ObjectMap}
The \type{Map} type does not exist at runtime and has been replaced with one of the above objects.
Map defines \tref{array access}{types-abstract-array-access} using its key type.
\paragraph{Related content}
\item See the \href{}{Map API} for details of its methods.
\item \href{}{Data structures tutorials and examples} in the Haxe Code Cookbook.
An \href{}{Option} is an \tref{enum}{types-enum-instance} in the Haxe Standard Library which is defined like so:
enum Option<T> {
It can be used in various situations, such as communicating whether or not a method had a valid return and if so, what value it returned:
\section{Regular Expressions}
Haxe has built-in support for \emph{regular expressions}\footnote{}. They can be used to verify the format of a string, transform a string or extract some regular data from a given text.
Haxe has special syntax for creating regular expressions. We can create a regular expression object by typing it between the \expr{\textasciitilde/} combination and a single \expr{/} character:
var r = ~/haxe/i;
Alternatively, we can create regular expression with regular syntax:
var r = new EReg("haxe", "i");
First argument is a string with regular expression pattern, second one is a string with \emph{flags} (see below).
We can use standard regular expression patterns such as:
\item \expr{.} any character
\item \expr{*} repeat zero-or-more
\item \expr{+} repeat one-or-more
\item \expr{?} optional zero-or-one
\item \expr{[A-Z0-9]} character ranges
\item \expr{[\textasciicircum\textbackslash r\textbackslash n\textbackslash t]} character not-in-range
\item \expr{(...)} parenthesis to match groups of characters
\item \expr{\textasciicircum} beginning of the string (beginning of a line in multiline matching mode)
\item \expr{\$} end of the string (end of a line in multiline matching mode)
\item \expr{|} "OR" statement.
For example, the following regular expression matches valid email addresses:
Please notice that the \expr{i} at the end of the regular expression is a \emph{flag} that enables case-insensitive matching.
The possible flags are the following:
\item \expr{i} case insensitive matching
\item \expr{g} global replace or split, see below
\item \expr{m} multiline matching, \expr{\textasciicircum} and \expr{\$} represent the beginning and end of a line
\item \expr{s} the dot \expr{.} will also match newlines \emph{(Neko, C++, PHP, Flash and Java targets only)}
\item \expr{u} use UTF-8 matching \emph{(Neko and C++ targets only)}
\paragraph{Related content}
\item See the \href{}{EReg API} for details about its methods.
\item \href{}{Haxe snippets and tutorials about regular expressions} in the Haxe Code Cookbook.
Probably one of the most common uses for regular expressions is checking whether a string matches the specific pattern. The \expr{match} method of a regular expression object can be used to do that:
Specific information can be extracted from a matched string by using \emph{groups}. If \expr{match()} returns true, we can get groups using the \expr{matched(X)} method, where X is the number of a group defined by regular expression pattern:
Note that group numbers start with 1 and \expr{r.matched(0)} will always return the whole matched substring.
The \expr{r.matchedPos()} will return the position of this substring in the original string:
Additionally, \expr{r.matchedLeft()} and \expr{r.matchedRight()} can be used to get substrings to the left and to the right of the matched substring:
A regular expression can also be used to replace a part of the string:
We can use \expr{\$X} to reuse a matched group in the replacement:
A regular expression can also be used to split a string into several substrings:
The \expr{map} method of a regular expression object can be used to replace matched substrings using a custom function. This function takes a regular expression object as its first argument so we may use it to get additional information about the match being done and do conditional replacement. For example:
\subsection{Implementation Details}
Regular Expressions are implemented:
\item in JavaScript, the runtime is providing the implementation with the object RegExp.
\item in Neko and C++, the PCRE library is used
\item in Flash, PHP, C\# and Java, native implementations are used
\item in Flash 6/8, the implementation is not available
Haxe includes a floating point math library for some common mathematical operations. Most of the functions operate on and return \type{floats}. However, an \type{Int} can be used where a \type{Float} is expected, and Haxe also converts \type{Int} to \type{Float} during most numeric operations (see \Fullref{types-numeric-operators} for more details).
Here are some example uses of the math library:
\paragraph{Related content}
\item See the \href{}{Math API documentation} for all available functions.
\item \href{}{Haxe snippets and tutorials about math} in the Haxe Code Cookbook.
\subsection{Special Numbers}
The math library has definitions for several special numbers:
\item NaN (Not a Number): returned when a mathematically incorrect operation is executed, e.g. Math.sqrt(-1)
\item POSITIVE_INFINITY: e.g. divide a positive number by zero
\item NEGATIVE_INFINITY: e.g. divide a negative number by zero
\item PI : 3.1415...
\subsection{Mathematical Errors}
Although neko can fluidly handle mathematical errors, like division by zero, this is not true for all targets. Depending on the target, mathematical errors may produce exceptions and ultimately errors.
\subsection{Integer Math}
If you are targeting a platform that can utilize integer operations, e.g. integer division, it should be wrapped in \emph{} for improved performance. The Haxe Compiler can then optimize for integer operations. An example:
var intDivision =;
\todo{I think C++ can use integer operatins, but I don't know about any other targets. Only saw this mentioned in an old discussion thread, still true?}
It is common to see \Fullref{lf-static-extension} used with the math library. This code shows a simple example:
\define{Lambda}{define-lambda}{Lambda is a functional language concept within Haxe that allows you to apply a function to a list or \tref{iterators}{lf-iterators}. The Lambda class is a collection of functional methods in order to use functional-style programming with Haxe.}
It is ideally used with \expr{using Lambda} (see \tref{Static Extension}{lf-static-extension}) and then acts as an extension to \type{Iterable} types.
On static platforms, working with the \type{Iterable} structure might be slower than performing the operations directly on known types, such as \type{Array} and \type{List}.
\paragraph{Lambda Functions}
The Lambda class allows us to operate on an entire \type{Iterable} at once.
This is often preferable to looping routines since it is less error prone and easier to read.
For convenience, the \type{Array} and \type{List} class contains some of the frequently used methods from the Lambda class.
It is helpful to look at an example. The exists function is specified as:
static function exists<A>( it : Iterable<A>, f : A -> Bool ) : Bool
Most Lambda functions are called in similar ways. The first argument for all of the Lambda functions is the \type{Iterable} on which to operate. Many also take a function as an argument.
\item[\expr{Lambda.array}, \expr{Lambda.list}] Convert Iterable to \type{Array} or \type{List}. It always returns a new instance.
\item[\expr{Lambda.count}] Count the number of elements. If the Iterable is a \type{Array} or \type{List} it is faster to use its length property.
\item[\expr{Lambda.empty}] Determine if the Iterable is empty. For all Iterables it is best to use the this function; it's also faster than compare the length (or result of Lambda.count) to zero.
\item[\expr{Lambda.has}] Determine if the specified element is in the Iterable.
\item[\expr{Lambda.exists}] Determine if criteria is satisfied by an element.
\item[\expr{Lambda.indexOf}] Find out the index of the specified element.
\item[\expr{Lambda.find}] Find first element of given search function.
\item[\expr{Lambda.foreach}] Determine if every element satisfies a criteria.
\item[\expr{Lambda.iter}] Call a function for each element.
\item[\expr{Lambda.concat}] Merge two Iterables, returning a new List.
\item[\expr{Lambda.filter}] Find the elements that satisfy a criteria, returning a new List.
\item[\expr{}, \expr{Lambda.mapi}] Apply a conversion to each element, returning a new List.
\item[\expr{Lambda.fold}] Functional fold, which is also known as reduce, accumulate, compress or inject.
This example demonstrates the Lambda filter and map on a set of strings:
using Lambda;
class Main {
static function main() {
var words = ['car', 'boat', 'cat', 'frog'];
var isThreeLetters = function(word) return word.length == 3;
var capitalize = function(word) return word.toUpperCase();
// Three letter words and capitalized.
trace(words.filter(isThreeLetters).map(capitalize)); // [CAR,CAT]
This example demonstrates the Lambda count, has, foreach and fold function on a set of ints.
using Lambda;
class Main {
static function main() {
var numbers = [1, 3, 5, 6, 7, 8];
trace(numbers.count()); // 6
trace(numbers.has(4)); // false
// test if all numbers are greater/smaller than 20
trace(numbers.foreach(function(v) return v < 20)); // true
trace(numbers.foreach(function(v) return v > 20)); // false
// sum all the numbers
var sum = function(num, total) return total += num;
trace(numbers.fold(sum, 0)); // 30
// get highest number
trace(numbers.fold(Math.max, numbers[0])); // 8
// get lowest number
trace(numbers.fold(Math.min, numbers[0])); // 1
\paragraph{Related content}
\item See the \href{}{Lambda API documentation} for all available functions.
Haxe comes with a standard template system with an easy to use syntax which is interpreted by a lightweight class called \href{}{haxe.Template}.
A template is a string or a file that is used to produce any kind of string output depending on the input. Here is a small template example:
The console will trace \ic{My name is Mark, 30 years old}.
An expression can be put between the \ic{::}, the syntax allows the current possibilities:
\item[\ic{::name::}] the variable name
\item[\ic{::expr.field::}] field access
\item[\ic{::(expr)::}] the expression expr is evaluated
\item[\ic{::(e1 op e2)::}] the operation op is applied to e1 and e2
\item[\ic{::(135)::}] the integer 135. Float constants are not allowed
It is possible to test conditions using \ic{::if flag1::}. Optionally, the condition may be followed by \ic{::elseif flag2::} or \ic{::else::}. Close the condition with \ic{::end::}.
::if isValid:: valid ::else:: invalid ::end::
Operators can be used but they don't deal with operator precedence. Therefore it is required to enclose each operation in parentheses \ic{()}. Currently, the following operators are allowed: \ic{+}, \ic{-}, \ic{*}, \ic{/}, \ic{>}, \ic{<}, \ic{>=}, \ic{<=}, \ic{==}, \ic{!=}, \ic{\&\&} and \ic{||}.
For example \ic{::((1 + 3) == (2 + 2))::} will display true.
::if (points == 10):: Great! ::end::
To compare to a string, use double quotes \ic{"} in the template.
::if (name == "Mark"):: Hi Mark ::end::
Iterate on a structure by using \ic{::foreach::}. End the loop with \ic{::end::}.
::foreach users::
To include templates in other templates, pass the sub-template result string as a parameter.
var users = [{name:"Mark", age:30}, {name:"John", age:45}];
var userTemplate = new haxe.Template("::foreach users:: ::name::(::age::) ::end::");
var userOutput = userTemplate.execute({users: users});
var template = new haxe.Template("The users are ::users::");
var output = template.execute({users: userOutput});
The console will trace \ic{The users are Mark(30) John(45)}.
\paragraph{Template macros}
To call custom functions while parts of the template are being rendered, provide a \expr{macros} object to the argument of \href{}{Template.execute}. The key will act as the template variable name, the value refers to a callback function that should return a \type{String}. The first argument of this macro function is always a \expr{resolve()} method, followed by the given arguments. The resolve function can be called to retrieve values from the template context. If \expr{macros} has no such field, the result is unspecified.
The following example passes itself as macro function context and executes \ic{display} from the template.
The console will trace \ic{The results: Mark ran 3.5 kilometers in 15 minutes}.
Use the \href{}{Template.globals} object to store values that should be applied across all \type{haxe.Template} instances. This has lower priority than the context argument of \expr{Template.execute}.
\paragraph{Using resources}
To separate the content from the code, consider using the \tref{resource embedding system}{cr-resources}.
Place the template-content in a new file called \ic{sample.mtt}, add \ic{-resource sample.mtt@my_sample} to the compiler arguments and retrieve the content using \expr{haxe.Resource.getString}.
When running the template system on the server side, you can simply use \expr{neko.Lib.print} or \expr{php.Lib.print} instead of trace to display the HTML template to the user.
\paragraph{Related content}
\item See the \href{}{Template API} for details about its methods.
Haxe supports runtime reflection of types and fields. Special care has to be taken here because runtime representation generally varies between targets. In order to use reflection correctly it is necessary to understand what kind of operations are supported and what is not. Given the dynamic nature of reflection, this can not always be determined at compile-time.
The reflection API consists of two classes:
\item[Reflect:] A lightweight API which work best on \tref{anonymous structures}{types-anonymous-structure}, with limited support for \tref{classes}{types-class-instance}.
\item[Type:] A more robust API for working with classes and \tref{enums}{types-enum-instance}.
The available methods are detailed in the API for \href{}{Reflect} and \href{}{Type}.
Reflection can be a powerful tool, but it is important to understand why it can also cause problems. As an example, several functions expect a \tref{String}{std-String} argument and try to resolve it to a type or field. This is vulnerable to typing errors:
However, even if there are no typing errors it is easy to come across unexpected behavior:
The problem here is that the compiler never actually ``sees'' the type \type{haxe.Template}, so it does not compile it into the output. Furthermore, even if it were to see the type there could be issues arising from \tref{dead code elimination}{cr-dce} eliminating types or fields which are only used via reflection.
Another set of problems comes from the fact that, by design, several reflection functions expect arguments of type \tref{Dynamic}{types-dynamic}, meaning the compiler cannot check if the passed in arguments are correct. The following example demonstrates a common mistake when working with \expr{callMethod}:
The commented out call would be accepted by the compiler because it assigns the string \expr{"f"} to the function argument \expr{func} which is specified to be \expr{Dynamic}.
A good advice when working with reflection is to wrap it in a few functions within an application or API which are called by otherwise type-safe code. An example could look like this:
While the method \expr{reflective} could internally work with reflection (and \type{Dynamic} for that matter) a lot, its return value is a typed structure which the callers can use in a type-safe manner.
Many runtime values can be serialized and deserialized using the \href{}{haxe.Serializer} and \href{}{haxe.Unserializer} classes. Both support two usages:
\item Create an instance and continuously call the \expr{serialize}/\expr{unserialize} method to handle multiple values.
\item Call their static \expr{run} method to serialize/deserialize a single value.
The following example demonstrates the first usage:
The result of the serialization (here stored in local variable \expr{s}) is a \tref{String}{std-String} and can be passed around at will, even remotely. Its format is described in \Fullref{std-serialization-format}.
\paragraph{Supported values}
\item \expr{null}
\item \type{Bool}, \type{Int} and \type{Float} (including infinities and \expr{NaN})
\item \type{String}
\item \type{Date}
\item \type{} (encoded as base64)
\item \tref{\type{Array}}{std-Array} and \tref{\type{List}}{std-List}
\item \type{haxe.ds.StringMap}, \type{haxe.ds.IntMap} and \type{haxe.ds.ObjectMap}
\item \tref{anonymous structures}{types-anonymous-structure}
\item Haxe \tref{class instances}{types-class-instance} (not native ones)
\item \tref{enum instances}{types-enum-instance}
\paragraph{Serialization configuration}
Serialization can be configured in two ways. For both a static variable can be set to influence all \href{}{haxe.Serializer} instances, and a member variable can be set to only influence a specific instance:
\item[\expr{USE_CACHE}, \expr{useCache}:] If true, repeated structures or class\slash enum instances are serialized by reference. This can avoid infinite loops for recursive data at the expense of longer serialization time. By default, object caching is disabled; strings however are always cached.
\item[\expr{USE_ENUM_INDEX}, \expr{useEnumIndex}:] If true, enum constructors are serialized by their index instead of their name. This can make the resulting string shorter, but breaks if enum constructors are inserted into the type before deserialization. This behavior is disabled by default.
\paragraph{Deserialization behavior}
If the serialization result is stored and later used for deserialization, care has to be taken to maintain compatibility when working with class and enum instances. It is then important to understand exactly how unserialization is implemented.
\item The type has to be available in the runtime where the deserialization is made. If \tref{dead code elimination}{cr-dce} is active, a type which is used only through serialization might be removed.
\item Each \type{Unserializer} has a member variable \expr{resolver} which is used to resolve classes and enums by name. Upon creation of the \type{Unserializer} this is set to \expr{Unserializer.DEFAULT_RESOLVER}. Both that and the instance member can be set to a custom resolver.
\item Classes are resolved by name using \expr{resolver.resolveClass(name)}. The instance is then created using \expr{Type.createEmptyInstance}, which means that the class constructor is not called. Finally, the instance fields are set according to the serialized value.
\item Enums are resolved by name using \expr{resolver.resolveEnum(name)}. The enum instance is then created using \expr{Type.createEnum}, using the serialized argument values if available. If the constructor arguments were changed since serialization, the result is unspecified.
\paragraph{Custom (de)serialization}
If a class defines the member method \expr{hxSerialize}, that method is called by the serializer and allows custom serialization of the class. Likewise, if a class defines the member method \expr{hxUnserialize} it is called by the deserializer:
In this example we decide that we want to ignore the value of member variable \expr{y} and do not serialize it. Instead we default it to \expr{-1} in \expr{hxUnserialize}. Both methods are annotated with the \expr{@:keep} metadata to prevent \tref{dead code elimination}{cr-dce} from removing them as they are never properly referenced in the code.
See \href{}{Serializer} and \href{}{Unserializer} API documentation for details.
\subsection{Serialization format}
Each supported value is translated to a distinct prefix character, followed by the necessary data.
\item[\expr{null}:] \expr{n}
\item[\type{Int}:] \expr{z} for zero, or \expr{i} followed by the integer display (e.g. \expr{i456})
\item[\type{Float}:] \mbox{}
\item[\expr{NaN}:] \expr{k}
\item[negative infinity:] \expr{m}
\item[positive infinity:] \expr{p}
\item[finite floats:] \expr{d} followed by the float display (e.g. \expr{d1.45e-8})
\item[\type{Bool}:] \expr{t} for \expr{true}, \expr{f} for \expr{false}
\item[\type{String}:] \expr{y} followed by the url encoded string length, then \expr{:} and the url encoded string (e.g. \expr{y10:hi\%20there for "hi there".}
\item[name-value pairs:] a serialized string representing the name followed by the serialized value
\item[structure:] \expr{o} followed by the list of name-value pairs and terminated by \expr{g} (e.g. \expr{oy1:xi2y1:kng} for \expr{\{x:2, k:null\}})
\item[\type{List}:] \expr{l} followed by the list of serialized items, followed by \expr{h} (e.g. \expr{lnnh} for a list of two \expr{null} values)
\item[\type{Array}:] \expr{a} followed by the list of serialized items, followed by \expr{h}. For multiple consecutive \expr{null} values, \expr{u} followed by the number of \expr{null} values is used (e.g. \expr{ai1i2u4i7ni9h for [1,2,null,null,null,null,7,null,9]})
\item[\type{Date}:] \expr{v} followed by the date itself (e.g. \expr{v2010-01-01 12:45:10})
\item[\type{haxe.ds.StringMap}:] \expr{b} followed by the name-value pairs, followed by \expr{h} (e.g. \expr{by1:xi2y1:knh} for \expr{\{"x" => 2, "k" => null\}})
\item[\type{haxe.ds.IntMap}:] \expr{q} followed by the key-value pairs, followed by \expr{h}. Each key is represented as \expr{:<int>} (e.g. \expr{q:4n:5i45:6i7h} for \expr{\{4 => null, 5 => 45, 6 => 7\}})
\item[\type{haxe.ds.ObjectMap}:] \expr{M} followed by serialized value pairs representing the key and value, followed by \expr{h}
\item[\type{}:] \expr{s} followed by the length of the base64 encoded bytes, then \expr{:} and the byte representation using the codes \expr{A-Za-z0-9\%} (e.g. \expr{s3:AAA} for 2 bytes equal to \expr{0}, and \expr{s10:SGVsbG8gIQ} for \expr{"Hello !")})
\item[exception:] \expr{x} followed by the exception value
\item[class instance:] \expr{c} followed by the serialized class name, followed by the name-value pairs of the fields, followed by \expr{g} (e.g. \expr{cy5:Pointy1:xzy1:yzg} for \expr{new Point(0, 0)} (having two integer fields \expr{x} and \expr{y})
\item[enum instance (by name):] \expr{w} followed by the serialized enum name, followed by the serialized constructor name, followed by \expr{:}, followed by the number of arguments, followed by the argument values (e.g. \expr{wy3:Fooy1:A:0} for \expr{Foo.A} (with no arguments), \expr{wy3:Fooy1:B:2i4n} for \expr{Foo.B(4,null)})
\item[enum instance (by index):] \expr{j} followed by the serialized enum name, followed by \expr{:}, followed by the constructor index (starting from 0), followed by \expr{:}, followed by the number of arguments, followed by the argument values (e.g. \expr{jy3:Foo:0:0} for \expr{Foo.A} (with no arguments), \expr{jy3:Foo:1:2i4n} for \expr{Foo.B(4,null)})
\item[cache references:] \mbox{}
\item[\type{String}:] \expr{R} followed by the corresponding index in the string cache (e.g. \expr{R456})
\item[class, enum or structure] \expr{r} followed by the corresponding index in the object cache (e.g. \expr{r42})
\item[custom:] \expr{C} followed by the class name, followed by the custom serialized data, followed by \expr{g}
\noindent Cached elements and enum constructors are indexed from zero.
Haxe provides built-in support for working with \emph{XML}\footnote{} data via the \href{}{haxe.Xml} class.
\subsection{Getting started with Xml}
\paragraph{Creating a root element}
A \type{Xml} root element can be created using the \expr{Xml.createElement} method.
var root = Xml.createElement('root');
trace(root); // <root />
An root node element can also be created by parsing a \type{String} containing the XML data.
var root = Xml.parse('<root />').firstElement();
trace(root); // <root />
\paragraph{Creating child elements}
Adding child elements to the root can be done using the \expr{addChild} method.
var child:Xml = Xml.createElement('child');
trace(root); // <root><child/></root>
Adding attributes to an element can be done by using the \expr{set()} method.
child.set('name', 'John');
trace(root); // <root><child name="John"/></root>
\paragraph{Accessing elements and values}
This code parses an XML string into an object structure \type{Xml} and then accesses properties of the object.
var xmlString = '<hello name="world!">Haxe is great!</hello>';
var xml:Xml = Xml.parse(xmlString).firstElement();
trace(xml.nodeName); // hello
trace(xml.get('name')); // world!
trace(xml.firstChild().nodeValue); // Haxe is great!
The difference between \expr{firstChild} and \expr{firstElement} is that the second function will return the first child with the type \type{Xml.Element}.
\paragraph{Iterate on Xml elements}
We can as well use other methods to iterate either over children or elements.
for (child in xml) {
// iterate on all children.
for (elt in xml.elements()) {
// iterate on all elements.
for (user in xml.elementsNamed("user")) {
// iterate on all elements with a nodeName "user".
for (att in xml.attributes()) {
// iterator on all attributes.
See \href{}{Xml} API documentation for details about its methods.
\subsection{Parsing Xml}
The static method \href{}{Xml.parse} can be used to parse \emph{XML} data and obtain a Haxe value from it.
var xml = Xml.parse('<root>Haxe is great!</root>').firstElement();
\subsection{Encoding Xml}
The method \href{}{xml.toString()} can be used to obtain the \type{String} representation.
var xml = Xml.createElement('root');
trace(xml.toString()); // <root><child1/><child2/></root>
\subsection{Simplified Xml access}
The \href{}{haxe.xml.Access} API exists to provide a dot-syntax access to the most common \type{Xml} methods.
Since Haxe 4 this class is named \type{haxe.xml.Access}. In Haxe 3 it was known as \type{haxe.xml.Fast}.
Here's an example of XML Access usage:
// parse some XML data
var xml = Xml.parse("
<user name='John'>
// wrap the Xml for Access
var access = new haxe.xml.Access(xml.firstElement());
// access attributes
trace(; // attribute "name"
if (access.has.age) trace( access.att.age ); // optional attribute
// access the "phone" child, which is wrapped with haxe.xml.Access too
var phone =;
// iterate over numbers
for (p in phone.nodes.number) {
This code works the same on all platforms.
There are different accessors that can be used with the Access API:
\item[\expr{.name}] returns the name of the current element (same as \expr{Xml.nodeName}).
\item[\expr{.x}] returns the current corresponding \type{Xml} node.
\item[\expr{.att.<name>}] access to a given attribute. An exception is thrown if the attribute doesn't exists.
\item[\expr{.has.<name>}] check the existence of an attribute.
\item[\expr{.elements}] the list of all sub-elements (which are the nodes with type \type{Xml.Element}).
\item[\expr{.node.<name>}] access to the first sub element with the given name. An exception is thrown if the element doesn't exists.
\item[\expr{.nodes.<name>}] returns a List of elements with the given name.
\item[\expr{.hasNode.<name>}] check the existence of a sub node with the given name.
\item[\expr{.innerData}] returns the inner \ic{PCDATA} or \ic{CDATA} of the node. An exception is thrown if there is no data or if there not only data but also other nodes.
\item[\expr{.innerHTML}] returns the XML string built with all the sub nodes, excluding the current one.
\paragraph{Self-closed nodes}
Please note that we cannot access self-closed nodes as "regular" ones.
For example, provided this XML:
var xml = Xml.parse("<xml>
var access = new haxe.xml.Access(xml.firstElement());
var myNode1Value = access.node.myNode1.innerData;
// no problem
var myNode2Value = access.node.myNode2.innerData;
// ERROR, an exception is thrown!
If the XML might contain self-closed nodes, check \expr{hasNode.innerData} before and default to an alternative value.
var myNode2Value = access.node.myNode2.hasNode.innerData ? access.node.myNode2.innerData : null;
Haxe provides built-in support for (de-)serializing \emph{JSON}\footnote{} data via the \ic{haxe.Json} class.
\paragraph{Related content}
\item See the \href{}{Haxe Json API documentation}.
\item \href{}{Haxe snippets and tutorials about JSON} in the Haxe Code Cookbook.
\subsection{Parsing JSON}
Use the \href{}{haxe.Json.parse} static method to parse \emph{JSON} data and obtain a Haxe value from it:
Note that the type of the object returned by \expr{haxe.Json.parse} is \expr{Dynamic}, so if the structure of our data is well-known, we may want to specify a type using \tref{anonymous structures}{types-anonymous-structure}. This way we provide compile-time checks for accessing our data and most likely more optimal code generation, because compiler knows about types in a structure:
\subsection{Encoding JSON}
Use the \href{}{haxe.Json.stringify} static method to encode a Haxe value into a \emph{JSON} string:
\subsection{Implementation details}
The \href{}{haxe.Json} API automatically uses native implementation on targets where it is available, i.e. \emph{JavaScript}, \emph{Flash} and \emph{PHP} and provides its own implementation for other targets.
Usage of Haxe own implementation can be forced with \expr{-D haxeJSON} compiler argument. This will also provide serialization of \tref{enums}{types-enum-instance} by their index, \tref{maps}{std-Map} with string keys and class instances.
Older browsers (Internet Explorer 7, for instance) may not have native \emph{JSON} implementation. In case it's required to support them, we can include one of the JSON implementations available on the internet in the HTML page. Alternatively, a \expr{-D old_browser} compiler argument that will make \type{haxe.Json} try to use native JSON and, in case it's not available, fallback to its own implementation.
Haxe remoting is a way to communicate between different platforms. With Haxe remoting, applications can transmit data transparently, send data and call methods between server and client side.
\paragraph{Related content}
\item See the \href{}{remoting package} on the API documentation for more details on its classes.
\subsection{Remoting Connection}
In order to use remoting, there must be a connection established. There are two kinds of Haxe Remoting connections:
\item[\href{}{haxe.remoting.Connection}] is used for \emph{synchronous connections}, where the results can be directly obtained when calling a method.
\item[\href{}{haxe.remoting.AsyncConnection}] is used for \emph{asynchronous connections}, where the results are events that will happen later in the execution process.
\paragraph{Start a connection}
There are some target-specific constructors with different purposes that can be used to set up a connection:
\item[All targets:]
Returns an asynchronous connection to the given URL which should link to a Haxe server application.
\item[\expr{ExternalConnection.jsConnect(name:String, ctx:Context)}]
Allows a connection to the local JavaScript Haxe code. The JS Haxe code must be compiled with the class ExternalConnection included. This only works with Flash Player 8 and higher.
\item[\expr{AMFConnection.urlConnect(url:String)} and \expr{AMFConnection.connect( cnx : NetConnection )}]
Allows a connection to an \href{}{AMF Remoting server} such as \href{}{Flash Media Server} or \href{}{AMFPHP}.
Allows remoting communications over an \type{XMLSocket}
Allows remoting communications over a \href{}{Flash LocalConnection}
\item[\expr{ExternalConnection.flashConnect(name:String, obj:String, ctx:Context)}]
Allows a connection to a given Flash Object. The Haxe Flash content must be loaded and it must include the \expr{haxe.remoting.Connection} class. This only works with Flash 8 and higher.
Will work like the asynchronous version but in synchronous mode.
Allows real-time communications with a Flash client which is using an \type{XMLSocket} to connect to the server.
\paragraph{Remoting context}
Before communicating between platforms, a remoting context has to be defined. This is a shared API that can be called on the connection at the client code.
This server code example creates and shares an API:
class Server {
function new() { }
function foo(x, y) { return x + y; }
static function main() {
var ctx = new haxe.remoting.Context();
ctx.addObject("Server", new Server());
// handle normal request
trace("This is a remoting server !");
\paragraph{Using the connection}
Using a connection is pretty convenient. Once the connection is obtained, use classic dot-access to evaluate a path and then use \expr{call()} to call the method in the remoting context and get the result.
The asynchronous connection takes an additional function parameter that will be called when the result is available.
This client code example connects to the server remoting context and calls a function \expr{foo()} on its API.
class Client {
static function main() {
var cnx = haxe.remoting.HttpAsyncConnection.urlConnect("http://localhost/");
cnx.setErrorHandler( function(err) { trace('Error: $err'); } );[1,2], function(data) { trace('Result: $data'); });
To make this work for the Neko target, setup a Neko Web Server, point the url in the Client to \ic{"http://localhost2000/remoting.n"} and compile the Server using \ic{-main Server -neko remoting.n}.
\paragraph{Error handling}
\item When an error occurs in a asynchronous call, the error handler is called as seen in the example above.
\item When an error occurs in a synchronous call, an exception is raised on the caller-side as if we were calling a local method.
\paragraph{Data serialization}
Haxe Remoting can send a lot of different kinds of data. See \tref{Serialization}{std-serialization}.
\paragraph{Related content}
\item See the \href{}{remoting package} on the API documentation for more details on its classes.
\subsection{Implementation details}
\paragraph{JavaScript security specifics}
The html-page wrapping the js client must be served from the same domain as the one where the server is running. The same-origin policy restricts how a document or script loaded from one origin can interact with a resource from another origin. The same-origin policy is used as a means to prevent some of the cross-site request forgery attacks.
To use the remoting across domain boundaries, CORS (cross-origin resource sharing) needs to be enabled by defining the header \ic{X-Haxe-Remoting} in the \ic{.htaccess}:
# Enable CORS
Header set Access-Control-Allow-Origin "*"
Header set Access-Control-Allow-Methods: "GET,POST,OPTIONS,DELETE,PUT"
Header set Access-Control-Allow-Headers: X-Haxe-Remoting
See \href{}{same-origin policy} for more information on this topic.
Also note that this means that the page can't be served directly from the file system \ic{"file:///C:/example/path/index.html"}.
\paragraph{Flash security specifics}
When Flash accesses a server from a different domain, set up a \ic{crossdomain.xml} file on the server, enabling the \ic{X-Haxe} headers.
<allow-access-from domain="*"/> <!-- or the appropriate domains -->
<allow-http-request-headers-from domain="*" headers="X-Haxe*"/>
\paragraph{Arguments types are not ensured}
There is no guarantee of any kind that the arguments types will be respected when a method is called using remoting.
That means even if the arguments of function \expr{foo} are typed to \type{Int}, the client will still be able to use strings while calling the method.
This can lead to security issues in some cases. When in doubt, check the argument type when the function is called by using the \expr{} method.
\section{Unit Testing}
The Haxe Standard Library provides basic unit testing classes from the \href{}{haxe.unit} package.
\paragraph{Creating new test cases}
First, create a new class extending \href{}{haxe.unit.TestCase} and add own test methods. Every test method name must start with "\ic{test}".
\paragraph{Running unit tests}
To run the test, an instance of \href{}{haxe.unit.TestRunner} has to be created. Add the \href{}{TestCase} using the \expr{add} method and call \expr{run} to start the test.
The result of the test looks like this:
Class: MyTestCase
OK 1 tests, 0 failed, 1 success
\paragraph{Test functions}
The \type{haxe.unit.TestCase} class comes with three test functions.
\item[\expr{assertEquals(expected, actual)}] Succeeds if \ic{expected} and \ic{actual} are equal
\item[\expr{assertTrue(a)}] Succeeds if \ic{a} is \expr{true}
\item[\expr{assertFalse(a)}] Succeeds if \ic{a} is \expr{false}
\paragraph{Setup and tear down}
To run code before or after the test, override the functions \expr{setup} and \expr{tearDown} in the \expr{TestCase}.
\item[\expr{setup}] is called before each test runs.
\item[\expr{tearDown}] is called once after all tests are run.
\paragraph{Comparing Complex Objects}
With complex objects it can be difficult to generate expected values to compare to the actual ones. It can also be a problem that \expr{assertEquals} doesn't do a deep comparison. One way around these issues is to use a string as the expected value and compare it to the actual value converted to a string using \expr{Std.string}. Below is a trivial example using an array.
public function testArray() {
var actual = [1,2,3];
assertEquals("[1, 2, 3]", Std.string(actual));
\paragraph{Run unit test}
This is an example showing how to run your unit tests (on Neko and Node.js) after compilation using a \tref{HXML}{compiler-usage-hxml}.
-cp source/main/haxe
-cp source/test/haxe
-main your.package.TestRunnerMain
-neko output/neko/test.n
-cmd neko ./output/neko/test.n
-js output/javascript/test.js
-cmd node ./output/javascript/test.js
\paragraph{Related content}
\item See the \href{}{haxe.unit} package on the API documentation for more details.