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

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...