Function's type

Functions are typed too. That's good for two reasons: you can pass a function as a parameter to another function, and you can also store typed functions in a variable!

Expressing a function's type

All functions in haXe are typed, so for example, when you write:

public function outputString(st : String) : Void;

we know that the outputString function takes one parameter of type String and returns Void (that actually means that it does not return any object). This is represented as the following type:

String->Void

Now, imagine the following function:

public function sumAndCeil(a : Float, b : Int) : Int

Its type is:

Float->Int->Int

Ok, this was the easy part.

Functions using functions

As mentioned earlier, a function can be given as an argument to another function. That also means that a function can take a function as an argument. This is useful, for example, to apply filters on lists or arrays. There's such a case in the standard library: List.filter. I will simplify things a little bit by saying that this method, on a list of String, could be declared as follows:

public function filter(f : String->Bool) : List<String>

As you may have guessed, this function takes a function as a parameter, passes each element of the list to it, if the function returns true, then it adds the element to a list that it returns.

The type of this function is:

(String->Bool)->List<String>

So, when you want to write the type of a function that uses functions, you simply need to put parentheses around the function's type.

Functions can also return a function; in this case, you simply need to put parentheses after the last arrow of the type:

public function takesStringReturnsFunction(s : String) : String->Bool

This function has the type:

String->(String->Bool)

It takes a String and returns a function that takes a String and returns a Bool. Calling the returned function would be as simple as writing:

takesStringReturnsFunction("Hello")("World");

Dynamic functions

You can mark a function as being "dynamic". When you do so, you will be able to replace a function with another one (with the limit that it should be of the same type).

Being able to do that may allow you to change the behavior of a particular instance of a class. This is a powerful feature, but you should remember that, on the other hand, it can be costly in terms of performances on some platforms.

The following is a simple example:

class TestFunction
{
public function new()
{
}
public static function main(): Void
{
var m = new TestFunction();
m.saySomething();
m.saySomething = function () {
trace("Hi.");
};
m.saySomething();
}
public dynamic function saySomething()
{
trace("Hello.");
}
}

If you run the preceding code, you will see that the first trace will be "Hello." while the other one will be "Hi.". This actually proves that we have been dynamically changing the behavior of our instance of TestFunction. Now, let's see what happens when we have two instances:

class TestFunction
{
public function new()
{
}
public static function main(): Void
{
var m = new TestFunction();
var n = new TestFunction();
m.saySomething();
m.saySomething = function () {
trace("Hi.");
};
m.saySomething();
n.saySomething();
}
public dynamic function saySomething()
{
trace("Hello.");
}
}

As you can see when running the code, the last trace will be "Hello.", proving that the modification of the function takes place at the instance level.

Note that the dynamic keyword can also be applied to static functions as illustrated by the following example:

class TestFunction
{
public function new()
{
}
public static function main(): Void
{
var m = new TestFunction();
var n = new TestFunction();
TestFunction.saySomething();
TestFunction.saySomething = function () {
trace("Hi.");
};
TestFunction.saySomething();
}
public static dynamic function saySomething()
{
trace("Hello.");
}
}

As you can see, this compiles and works as expected. Note however that overriding a dynamic function is impossible. It is also impossible to use super in a dynamic function.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.7.131