Monthly Archives: August 2009

Friendly types with haXe

If you are experienced with haXe you’ll know that its type system permits to catch a lot of errors at compile time simply veryfing that parameter/member types are correctly used in your application. Members can only have two access modifiers: public or private. But what if you want to emulate something like the “friend” modifier that exists in other languages?

Let’s see a practical example.

class Test {
  static function main() {
    var c = new Container();
    var n1 = c.add(new Item("John"));
    var n2 = c.add(new Item("Jane"));

    trace(c);
    trace(n1);
    trace(n2);
  }
}

class Container {
  public function new() {
    children = new List();
  }
  public function add(n : Item) {
    children.add(n);
    return n;
  }

  public inline function iterator() { return children.iterator(); }
  public inline function count() { return children.length; }

  public function toString() {
    return "[Container] has " + children.length + " children";
  }

  var children : List;
}

class Item {
  public var parent(default, null) : Container;
  public var name(default, null) : String;
  public function new(name : String) {
    this.name = name;
  }
  public function toString() {
    if(parent != null)
      return "[Item] " + name + " has " + (parent.count() - 1) + " sibblings";
    else
      return "[Item] " + name + " has no parent";
  }
}

This flat hierarchy proves my point because something is missing, the Item instances never receive a reference to their container and the field parent is always null. The output trace is:

Test.hx:8: [Container] has 2 children
Test.hx:9: [Item] John has no parent
Test.hx:10: [Item] Jane has no parent

I don’t want to expose parent as public or pass it in the Item constructor because I want my hierarchy to be composed just using the method add (in real life a lot of things can lead to that decision).
With a friend I could just add this to Item:

// friend method
private function setParent(p) {
  parent = p;
}

But how do you call that safely from outside the Item class? It is private so it cannot be invoked directly … you can use untypedto make the compiler skip the types check on that call; but what happens if you want to change the name of setParent toset_parent at a later point and forget to change the untyped call too? The compiler will not spot the error and you’ll only catch it at runtime when some strange behavior will pop.

Here it comes the solution, change the Container.add function this way:

public function add(n : Item) {
  children.add(n);
  var p : { private function setParent(p : Container) : Void } = n;
  p.setParent(this);
  return n;
}

I have created a temporary var p whose type is exactly and only what we need to access the “friend” method. It works without any casting or untyped access because the type of “p” is structurally a sub-type of Item making it implicitly compatible. We can callsetParent from p because typedefs define a structure but do not impose any access limit.

Now the output is what expected at first:

Test.hx:7: [Container] has 2 children
Test.hx:8: [Item] John has 1 sibblings
Test.hx:9: [Item] Jane has 1 sibblings

If and when you will change the name of Item.setParent the compiler will throw an error notifying that Item is no more compatible with the type of p.
Many thanks to Nicolas Cannasse that suggested this unusual way of using typedefs on the haXe mailing list.

 

Optimize cache for autoloading classes in haXe/PHP

The code generated using haXe/PHP uses the PHP autoloading feature to dynamically load classes definitions when they are required. This method has many advantages but a drawback too; the thing is that the autoloading function (hidden in php.Boot) needs to scan the lib folder to look for type definitions. It is not really a big work since only the file names are read without the need to immediately open the files, nevertheless this work is made each time the page is accessed. To reduce the impact of this operation you can simply create a cachefolder at the same level of lib and give it the proper attributes for PHP being able to write files in there. On the next use of your script, a new file haxe_autload.php will be automatically generated and will contain all the info required by the autoloading function.

As you can guess, the impact of this feature is proportional to the number of classes included in your project.

Be warned that it is better to use the cache only on production sites because during development it is quite normal adding new classes and the cache will not include them unless thehaxe_autload.php file is manually deleted. If you need to upgrade your production code, remember to delete that file first!

edit: I’ve made a few informal tests compiling a project that generates 76 files but uses only a few of them to maximize the impact of the cache trick. The results are the following but remember that your mileage may vary greatly and you can also find out that you will not have any benefit from abilitating the cache feature.

Execution time:
198ms no cache
151ms cache activated (31% speed gain)

 

haxe.Stack for PHP

Starting from haXe 2.04 the PHP generator supports the -debug switch. The implementation is almost identical to the haXe/JS one where the function calls are stored in a stack. You can see it working when you have an uncaught exception or using the haxe.Stack methods as in the following example:

import haxe.Stack;
class Test {
  static function main() {
    var f = function() { ref(); };
    f();
  }

  static function ref() {
    throw "error";
  }
}

The output will be:

uncaught exception: error

Called from Test::ref
Called from Test::main@4
Called from Test::main

Note that @4 means that a local function is involved.
You can obtain the same output without generating an exception replacing throw "error" with:

trace(Stack.toString(Stack.callStack()));

Is it appropriate to remove the -debug switch when compiling for deployment since it adds extra code to your output.