Book Image

haXe 2 Beginner's Guide

5 (1)
Book Image

haXe 2 Beginner's Guide

5 (1)

Overview of this book

Table of Contents (21 chapters)
haxe 2
Credits
Foreword
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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 User
{
   var sayHello : String->Void;
   
   public function new()
   {
      sayHello =    function(to : String)
               {
                  trace("Hello...