Blocks

Blocks are important things in haXe, as they can help you write things that might be complicated in some languages in an easy way. Blocks are delimited in the C-style by the { and } characters. What makes them special in haXe is that they have a value and a type. A block's value and type is those of the last expression of this block. Note that empty blocks are of Void type. This type is used to indicate that no value is going to be returned.

Here's an example on how that may ease writing in some cases:

public static function main() : Void
{
var s : String;
s = if(true)
{
"Vrai";
} else
{
"Faux";
}
trace(s);
}

In this example, it will always trace Vrai (which is the French translation of "True"), but I think you get the idea. In most other languages, you would have to write something like:

public static function main() : Void
{
var s : String;
if(true)
{
s = "Vrai";
} else
{
s = "Faux";
}
trace(s);
}

That's more typing and may well become less readable in complex cases. This can bring some sort of functional programming paradigm to haXe. In this example, the case is fairly simple and remains easy to understand in both cases, but in many complex cases you will learn to love haXe's blocks.

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

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