O'Reilly logo

haXe 2 by Benjamin Dasnois

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Local functions

Local functions are functions without a name. (often named "anonymous functions") They are values and as such can be assigned to any variable. The following is an example:

public class User
{
var sayHello : String->Void;
public function new()
{
sayHello = function(to : String)
{
trace("Hello" + to);
};
}
}

Local functions can access any local variable declared in the same scope as static variables, but cannot access the this variable.

Local functions are typed. The local function in the preceding example is typed as String-> Void. A function that takes a String, an Int, and returns a String would be typed as String ->Int -> String.

So, continuing the previous example, one could call the function in the following way:

public class ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required