Tag Archives: haxe

Functional or OOP? a simple case

Lately i am spending some time thinking how I could better organize my thx library. I have many ideas and simplifications in mind but in some areas I still have doubts.

Let’s take the simple interpolation case. As by Wikipedia definition, “interpolation is a method of constructing new data points within the range of a discrete set of known data points”. In programming terms it usually reduces to answer to questions like “if I have the values A and B, respectively the min and max on a scale,  what is the value of C at X percent on that scale and according to a certain equation?”. To further simplify take a scale between 10 and 100, what is the value at 40% using a linear equation?

The code for that can be something like this.

static function linearInterpolation(min : Float, max : Float, k : Float)
  return min + (max - min) * k

There is nothing wrong with this function but if you want to repeat the operation several times there are a couple of hiccups. First the operation computes delta (max – min) too many times; second, and most importantly, it requires that you keep track of variables (min and max) that are needed to define the interpolation but not needed to execute it.

The functional approach to this:

static function linearInterpolationF(min : Float, max : Float)
  var delta = max - min;
  return function(k : Float) return min + delta * k;

The usage is very simple:

var interpolate = linearInterpolationF(10, 100);
for(i in 0...10)

The code itself is clear but their type definitions do not say much:

typedef interpolateFunction<T> = (Float -> Float) -> Float -> T;
typedef interpolate<T> = Float -> T;

T represents the interpolated value that can be literally anything (a Float like in the examples above, or a color or …).

The OOP approach would be something like this:

class LinearInterpolator
  var min : Float;
  var delta : Float;
  public function new(min : Float, max : Float)
    this.min = min;
    this.delta = max - min;
  public function interpolate(k : Float) return min + delta * k

Needless to say that this version is much more verbose but the signature is in my opinion slightly more readable:

typedef Interpolator<T> = {
  interpolate : Float -> T

Usage is not very much different:

var interpolator = new LinearInterpolator(10, 100);
for(i in 0...10)

So from a user point of view the 2 approaches are not that different in terms of lines of code. It is also worth noting that transforming the OOP approach into the functional one is very very easy:

var interpolate = new LinearInterpolator(10, 100).interpolate;

The opposite is also possible but is a little more cumbersome for the user.

Performance wise the OOP approach wins hands down. Passing functions around is still taxing for all of the Haxe targets (not a Haxe limitation to be clear). In JavaScript, the OOP implementation is about 29% faster than the functional implementation when recycled many times (the same interpolation is reused millions of times) and the difference is even bigger (313%) when the interpolation is used only once. These numbers become even more critical for other platform … for CPP we have even more difference, 548% and 741% faster respectively.

Choosing a programming “style” is obviously not only about performances and in most projects the number above are meaningless; never the less when building a library performances are still an issue because users expect them ;)

Now that callback can be used on any number/position of arguments it is really easy to implement the functional approach from the basic implementation of linearInterpolation:

var interpolate = callback(linearInterpolation, 10, 100, _);

The difference in performances with linearInterpolationF is really minimal and it allows to switch to “functional mode” in a snap.

In this post I obviously oversimplified the case since the interpolation is bounded between two values and it is probably smarter to keep the equation as an argument instead of hard-coded. Interpolating on a path for example might require several points and several equations if curves are involved.

In thx we almost always favor the functional approach + a direct function (like in the first example) but I am not sure it is the “right” way.

What’s your take, what is the approach you like the most?

Haxe 2.09 – WWX 2012

It was really a great pleasure to attend the WWX 2012 in Paris. The talks were extremely interesting and they really showed how much the community is growing. Several factors are contributing to that growth and I am pretty sure that one of these is the amazing development that NME is living and the features that it offers in the mobile department.

The conference was also an occasion for announcements like the new formal spelling of “Haxe” (no more “haXe”). And of course the new Haxe 2.09 that is, in my opinion, one of the best releases ever. Here is my 2 cents about why I like this release so much.

Haxe compiler service

The idea of the Haxe compiler service is really simple. Instead of restarting the compilation process (and the compiler boot strap) on each execution, the compiler is always active in a service waiting for some code to be compiled. It go further than that (otherwise the difference in performances would be minimal), it also keeps cached in memory the intermediate states of the compilation updating only the portions that are subjects to modifications.

My personal benchamarks show the following (after the first run):

project standard compilation compilation service
utest test cases (neko) 310ms (w/o generation 210ms) 110s (w/o generation 10ms)
ReportGrid charts (js) 720ms (w/o generation 590ms) 490s (w/o generation 360ms)

Since the compiler was already very fast, what really makes this feature stand out is that it accelerates auto-completion through -display considerably. I’ve not tested it myself but Nicolas claims x10 faster performances. FlashDevelop is the only IDE so far having adopted this feature transparently but it is not yet in an official build. You can still use it by launching the service this way:

haxe --wait 888

(add -v if you want more detailed information)

And compile you project this way:

haxe --connect 888 build.hxml

Of course you can customize the host/port number and the .hxml file.

JS Improvements

The first most impressive improvement in the JavaScript target is certainly the introduction of --js-modern and the consequent removal of --js-namespace. The new command option enclose the generated code into a closure that prevent the pollution of the global object with all the types defined in your application. Beside that, it adds the “strict mode” directive that can potentially enhance performances in some JavaScript contexts. Also the way that fields are now written in the generated output are more human friendly than before.
Besides the new output works wonder with Google Closure Compiler:

project plain DCE DCE + Closure Compiler
ReportGrid charts 918kb 748kb 569kb
ReportGrid charts w/ –js-modern 924kb 755kb 483kb

Then there is the new -debug implementation. What it does is mapping the source Haxe code to the JavaScript output using a source mapping file. In the browsers that support this very new feature (Chrome so far) you will be able to debug your application directly in Haxe instead of in JavaScript. That means being able to set breakpoints, inspect variable values and call stacks directly in the Haxe code.

revamped Dead Code Elimination (DCE)

My first implementation of DCE had issues under certain conditions. Nicolas took over and revamped it completely making it a lot, lot better.
Using DCE you always have to use some care preventing that portions of the code you are using in your app are not removed by accident. For example if you create an instance by using reflection and the class name, then DCE is probably going to remove that class entirely since its typing is never encountered in the code flow. To avoid that to happen you must use @:expose meta tag at either the type level or at the method level.

Anonymous Objects

The anonymous objects have seen two major improvements, the first is the possibility to use quoted fields to be able to use special characters in your field name and the addition of the optional fields.
The first is very important because now you can simply copy and paste some Json object in your code without having to worry about stripping the quotes. To access the fields with special characters you still need to use reflection.
The optional fields are really powerful for “option objects” (pretty common in the JS world) where you want to be able to define a set of key/value pairs without the need of specifying all of them.
Note that this feature only applies to constant struct, for that reason you can do this:

// assuming that the type of the argument of 'create' is : { ?x : Int, ?y : Int, ?r : Int }
 create({ x : 10, y : 7, r : 4 });
 create({ x : 20, r : 5 });

but you cannot do this:

var options = opt(); // where opt is a function returning { x : 20, r : 5 };
create(options); // doesn't compile because not all the fields of options match the signature of the options argument

Unification of Classes

When Haxe was really young there was only one platform (neko) that was capable of accessing the system platform. With the proliferation of the new targets and the expanded possibilities to use the existing ones in different contexts, more and more implementations of the same functionalities have been spreaded over different package names (mainly neko, php and cpp). In Haxe 3 the platform specific API for these will be gone and they will be unified; in 2.09 the types have been already moved to their final destination but shortcuts were left so that the existing code doesn’t break.
The modules affected by these changes are:

Sys (was neko.Sys/php.Sys/cpp.Sys)

In thx there were already shortcuts to avoid platform specific packages and I guess I will have to remove them now.


This tiny macro is a huge time saver that allows you to simplify things like this:

var s = "Hi "+name+" "+lastname+", your score is "+score;

to this:

var s = Std.format("Hi $name $lastname, your score is $score");

A lot more readable and starting with Haxe 3 it will be applied automatically to all the string definitions compacting the above to:

var s = "Hi $name $lastname, your score is $score";

You can use any Haxe expression inside the string, but if it is more complex than a simple identifier you will have to use the curly brackets format:

Std.format("list length: ${list.length}")

std package for root package

In rare occasions you might have experienced conflicts between types with the same name but that existed in the root package and in a sub-package. Suppose that you have a class def.Type referenced in your code and in the same code you want also to use the type Type. The only way to make that happen would be to create a typedef alias to Type but now you can instead just use std.Type.


For performance reasons the String type is mapped directly to the native implementation of each target platform. The biggest implication of this choice is that the length property and the substr() method can return different results depending on the support of the UTF8 encoding in the targets. With haxe.Utf8 you can reliably obtain a consistent behavior cross-platform.

I have not covered all changes in the new release but only those that make me particularly happy :)

Vector Graphics in the Browser – WWX 2012

I have uploaded my slides (PDF) and the samples from the presentation:

… and remember, circles are bad!

The libraries presented are dhx and thx.

thx: error

The thx.error package contains a bunch of classes to manage errors in your applications. The base class is Error and is a lightweight container for error messages. The nice thing about it is that it separates the message itself from its parameters allowing to format it in a meaningful way:

var error = new Error("invalid import {0:C}", 0.123);
// trace results is: invalid import $0.12

If you provide the needed infrastructure you can also use an instance of ITranslation to deliver your messages:

// Translated Error
var translator = new DictionaryTranslation(ItIT.culture);
translator.addSingular("invalid import {0:C}", "importo invalido {0:C}");
// trace result is: importo invalido € 0,12

I often use to mark my abstract methods so that at least at runtime I have a feedback that the implementation is missing and I do that using the AbstractMethod error class (the NotImplemented error class is functionally equivalent but has a different semantic).

// AbstractMethod / NotImplemented
// in a real world context this will be an instance method and not an inline function

function notImplemented() throw new NotImplemented();
} catch (e : Dynamic) {

The nice thing is that I don’t have to type any message into the constructor because all the info I need are already in there; this is the trace:

method Main.notImplemented() needs to be implemented

Finally the NullArgument error class has a couple of static constructors that helps me check for null arguments. Using macros the argument name is extracted so that you don’t need to type it as a string; that spares you some typing but better yet avoids annoying and error prone repetitions.

// NullArgument
function toUpperCase(text : String)
	return text.toUpperCase();

} catch(e : Dynamic)
// trace result is: invalid null argument 'text' for method Main.toUpperCase()

The NullArgument.throwIfNullOrEmpty works similarly but also test for emptiness … this works on Strings, Lists, Arrays (length) and objects (no fields). Note that the type discovery is made at compile time and not at runtime:

// null or empty
function pop(arr : Array) : String
	return arr.pop();
} catch(e : Dynamic)

// trace result is: invalid null or empty argument 'arr' for method Main.pop()

thx: parsing dates

One of the latest additions to thx is thx.dates.DateParser. It is obviously a date parser, the nice thing about it is that it tries to be fuzzy and to extract a date whenever possible. You can try it yourself here:

Examples of expressions:

  • today
  • now
  • tomorrow
  • next april
  • last sunday
  • three weeks ago plus 5 minutes
  • tomorrow in the evening
  • 6/27
  • 27/6

To produce this tiny demo you just need a bunch of lines of code:

import thx.date.DateParser;
import thx.js.Dom;
import js.Dom;
class Main 
  static function main() 
    Dom.select("#date").onNode("keyup", function(n : HtmlDom, _) {
      var t : Text = cast n;
      var result = try {
        Dates.format(DateParser.parse(t.value), "DT");
      } catch (e : Dynamic) {

Surely the expression grammar can be improved but it works for my needs right now and it is already very flexible.

thx : Arrays

In thx there are many helper methods. The convention is that if a class is not meant to be instantiated and its only purpose is to add functionalities to other types than it will be named after the type it enhances but in the plural form. So it is now pretty obvious that Arrays is a helper class for the Array type as Strings is a helper class for String.

So far I am really focused to add (and to improve) features and I am not really worry about file size or performances. Some of the helper classes are pretty dense and refers to other dense definitions which can add some weight to your output. This issue will be addressed at some point by improving thx compatibility with haxe DCE (Dead Code Elimination) or maybe splitting the classes.

Back on the post topic, I’d like to introduce a few of the options you can find in Arrays. So I assume for the following examples that using Arrays; is always in place since that makes the programmer life even easier.

Arrays adds some functions to enhance method chaining, for example you can write:

var arr = [].add(1).add(2).addIf(value > 2, 3).remove(2);

Many methods in Arrays derive from the Lambda class found in the std library but tend to be more flexible and the definitions are also repeated in Iterators and Iterables to make your life easier. Whenever is possible inline is used to prevent adding a new level of indirection. Note that if a method returns a list, it always returns an Array, even if you are using the Iterator or Iterable type. Arrays are pretty fast on all the platforms and are a lot more flexible than the iterators.

// Ints.range(10) returns an array of int from 0 to 9
var arr = Ints.range(10)
  .filter(function(d) return 0 == d % 2)
  .map(function(d,i) return d*d);
// arr is [0,4,16,36,64]

Since hxculture has been included inside thx, you can format many types (and Array is not an exception) to string in a localized fashion. So far you can use 2 formats with array J for “join” and C for “count”.

trace(arr);  //  [0,4,16,36,64]
trace(arr.format("J")); // 0, 4, 16, 36, 64
// the first parameter is for the format of each individual value
trace(arr.format("J", ["D"]));  //  0.00, 4.00, 16.00, 36.00, 64.00
// the second parameter is for empty array
trace([].format("J", ["D", "-"]));  //  -
// the third parameter is the separator
trace(arr.format("J", ["D", "-", "; "]));  //  0.00; 4.00; 16.00; 36.00; 64.00
// the fourth parameter is the max number of elements to display
trace(arr.format("J", ["D", "-", "; ", "3"]));  //  0.00; 4.00; 16.00 ...
// the fifth parameter is for the remaining elements after max
trace(arr.format("J", ["D", "-", "; ", "3", " and more"]));  //  0.00; 4.00; 16.00 and more

trace(arr.format("C"));  //  5

You can also create a format function using var f = Arrays.formatf("J", ["D"]); and after that f([1,2,3]);

Functions like .all()/.any() permits to check if all or at least one of the elements of the array matches a certain rule. The rest of the functions in Arrays are pretty obvious to catch but if you have any doubt I will be glad to five more insights.

thx: color

thx is really growing fast and since it is not yet documented I want to start giving users some hints on the classes included.

The thx.color package is the first set of features I will introduce since the API is pretty stable and I use it a lot.

Creating a color from its channels is really easy:

var color = new Rgb(255, 0, 0); // RGB values are integers from 0 to 255

Of course you can use alternatives to create a Rgb color:

color = Rgb.fromInt(0xFF000);


color = Rgb.fromFloats(1.0, 0.0, 0.0);

… but if you are as lazy as I am you can use:

color = NamedColors.red;

or even

color = Colors.parse("red");

Note that Colors.parse() will try everything to find a matching color, infact you can pass a:

  • color name, as defined in NamedColors; names with spaces are accepted aswell (ei: “dark sea green“)
  • css color: #ff0000
  • rgb definition: rgb(255,0,1)
  • hsl definition: hsl(0,1,0.5)
  • cmyk definition: cmyk(0,1,1,0)

Other than Rgb you can define colors in the following colorspaces: Hsl, Grey, Cmyk

I find Hsl particularly useful when you want to create smooth interpolations:

using thx.colors.Hsl;
// ...
var midcolor = Hsl.interpolate(NamedColors.red.toHsl(), NamedColors.blue.toHsl(), 0.5);

If you want to recycle the interpolation and generate an array of colors you can use:

using Arrays;using thx.colors.Hsl;
// ...
var f = Hsl.interpolatef(NamedColors.red.toHsl(), NamedColors.blue.toHsl()),	len = 10;
var colors = Ints.range(len).map(function(_, i) return f(i / (len-1)));

The interpolate/interpolatef methods in Colors will do the same but starting from string definitions.

This can be very handy when you want to interpolate a CSS color. The Rgb.contrast/constrastBW methods are also pretty useful. The former will try to find a pastel color that contrasts with the passed one while the latter will always return white or black. The algorithm of contrastBW uses the grey equivalent of the passed color to estabilish the better contrast for the human vision.

ufront/uform/thx demo from haxecon 2011

Eventually I’ve found the time to put everything together and here come my demos from haxecon 2011.

You can play with it live or you can download the project and compile/run it on your machine.

To compile the project you must have installed through haxelib the following libraries (be sure you are using the correct version because the API will probably change before the 1.0 release):

Note that uform is included in the project and you don’t need to install anything for it.

To run the example on your machine you will probably have to tweak a couple of things:

  • Main.hx: if you use mod_rewrite you will have to create your .htaccess file and change the parameter in the Config constructor from “false” to “true”. Also, if you run the demo from a subfolder different then demo you will have to change that parameter too; if you plan to run in root just put “/” in the path argument.
  • demo/Config.hx: Change your connection settings for your Mysql database and be sure to create a table in the DB with the structure described in the same file.

The project is divided into 3 separate projects:

  • web-pages (both php and neko), use the haxecon.php.hxml or haxecon.neko.hxml to compile
  • colors example page (js), compile using haxecon.d3colors.hxml
  • d3.js port examples (js), compile using haxecon.d3examples.hxml

The live demo on this website is running on neko which is way faster than the php version.

Friend types are slow … really?

If you followed the post about friend types or the discussion about that topic on the haXe mailing list started by Michael Baczynski, you may believe that friend types can be slow. This is true (we are talking about targeting flash 9 here) if you use a temporary variable to “cast” the friend instance, but there is a workaround for that.

So suppose you have this class:

class Test
  var value : Int; // this is the private field we want to expose as friend
  public function new() {
    value = 1;

The friend type is:

typedef TestFriend = {
  private var value : Int;

So what are your options?

The simple one:

var test = new Test();
var friend : TestFriend = test;
var v = friend.value;

This is not fast because test needs to be converted to a dynamic type to be used like that. Here is the alternative, first create an utility method as such:

class TestUtil {
  public static inline function getValue(t : TestFriend) {
    return t.value;

Then use it:

var test = new Test();
var v = TestUtil.getValue(test);

Does it perform better? A lot (even faster that untyped). Is it type safe? Sure … no need for untyped.

Using “using” makes it even nicer:

using TestUtil;
// ...
var test = new Test();
var v = test.getValue();

About perfomances, I’ve made a test on 2.000.000 iterations (average on 5 cycles) and here are the results:

  • using a temp var (reassigned on each iteration): reference time (454.0 ms)
  • using a temp var (assigned once): 14% faster (397.8 ms)
  • untyped46% faster (310.6 ms)
  • using the static getValue() function: 50% faster (301.8 ms)


Attributes with haXe (AKA Metadata or Annotations)

Many languages support metadata in the form of additional information attached to classes, methods or variables. In this post I provide a sample implementation to produce such a thing in haXe.
I created a class Attributes (source code attached to this post) with the following signature:

class Attributes {
public static function of(cls : Class, ?field : String) : Attributes;
public var parent(default, null) : Attributes;
public function exists(key : String) : Bool;
public function get(key : String, ?alternative : String) : String;
public function set(key : String, value : String) : Void;
public function isInherited(key : String) : Bool;
public function keys() : Iterator;
public function iterator() : Iterator;
public function toString() : String;

Instances of the class cannot be created directly but they must be obtained using the static method Attributes.of(). The first parameter is the type of the class we want to retrieve attributes, the second optional parameter is a field of that class (it works with either static or instance members). The class is very similar to Hash with the exception that attributes are inherited; so class B that extends class A can inherit or redefine the attributes of B.

Let’s see how we can use the class.

import attributes.Attributes;
import haxe.rtti.Infos;

* @author Franco Ponticelli
* @year   2009
class Main implements Infos {
* @role entry point
static function main() {
trace("attributes of Main: " + Attributes.of(Main));
trace("attributes of Main.main: " + Attributes.of(Main, "main"));

The output is:

Main.hx:15: attributes of Main: { author : Franco Ponticelli, year : 2009 }
Main.hx:16: attributes of Main.main: { role : entry point }

In the first case we have retrieved the attributes of the class Main while in the second the attributes of its method main().

If you want to try you can’t forget about two things: classes that provide attributes must implement haxe.rtti.Infos and you must add the directive -D use_rtti_doc when you compile your project. The latter informs the compiler that nodes must be included in the generated code.

One nice thing about attributes is that they are both “code” and documentation. Attributes can have a lot of applications, in ORM they can be used to wire a class name to a SQL table name or to map fields and relations to columns; in controllers they can be used to bind a method to certain credentials or templates; in a unit testing system they could be used to mark the functions that are test methods.

There is one thing are a couple of things to consider before embracing this technique, rtti info are stored as XML in the code and the parsing can be slow (the provided implementation caches the results to speed things up) and there is currently a bug in the -D use_rtti_doc: overrode methods do not bring their documentation so attributes attached to them are simply discarded. Hopefully the latter problem will be addressed soon. (bug fixed in CVS, version 2.05 will be bug free)

download here: Attributes.v.1.0