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

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