Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
haXe 2 Beginner's Guide

You're reading from   haXe 2 Beginner's Guide Develop exciting applications with this multi-platform programming language

Arrow left icon
Product type Paperback
Published in Jul 2011
Publisher
ISBN-13 9781849512565
Length 288 pages
Edition Edition
Languages
Arrow right icon
Toc

Table of Contents (21) Chapters Close

haxe 2
Credits
Foreword
About the Author
About the Reviewers
www.PacktPub.com
Preface
1. Getting to know haXe FREE CHAPTER 2. Basic Syntax and Branching 3. Being Cross-platform with haXe 4. Understanding Types 5. The Dynamic Type and Properties 6. Using and Writing Interfaces, Typedefs, and Enums 7. Communication Between haXe Programs 8. Accessing Databases 9. Templating 10. Interfacing with the Target Platform 11. A Dynamic Website Using JavaScript 12. Creating a Game with haXe and Flash Pop Quiz Answers Index

Time for action – Interacting with the user


Now, we're going to create an application that takes your name as a parameter, greets you, and displays the number of characters in your name.

  1. Copy the following code in a file named Main.hx

    class Main
    {
      public function new()
      {
      
      }
    
      public static function main()
      {
       var name = neko.Sys.args()[0];
       trace("Hello " + name);
       trace("Your name is " + Std.string(name.length) + " characters long.");
      }
    }
  2. Open a terminal; go to the directory where you saved Main.hx and type the haxe –main Main -neko characterCounter.n command.

  3. Type the command (this is an example with my first name):

    neko characterCounter.n Benjamin

  4. You will see the following output:

    Main.hx:12: Hello Benjamin

    Main.hx:13: Your name is 8 characters long.

What just happened?

The program read your name from the command line, greeted you by appending your name to "Hello", and displayed the number of characters that make up your name.

  • The code: You already know the basics about the class and the main function. What is interesting to see here is how we get the arguments. When targeting neko, we can use the args function of neko.Sys. This function takes no parameters and returns an Array of String.

    As you can see, we can access an array's item by its index using the square-brackets notation. Here we are accessing the item at index 0 (arrays are 0-based, that means that the first item is always at index 0).

    Then, we use the trace function that you've already seen to display "Hello" followed by the name of the user. As you can see here, string concatenation is done by using the + operator. It is still quite important to note that there are classes such as StringBuf that can be used to achieve the same behavior, if you are focused on performances.

    You'll also notice that String has a variable named length that allows you to retrieve the number of characters in it.

    By the way, haXe is typed, and here we are using Std.string to convert the length of the string from Int to String. Std.string can be used to convert any value to a String. This is not mandatory here, as when using the + operator, if one of the two values is not an Int nor a Float, the operator will return a String. In this case, all operands will be considered as String.

  • The compilation: Well, as you can see, there's absolutely nothing new in the compilation process. The only thing we've changed is the output file's name.

  • Running the neko code: In the third step, we invoke the neko VM and tell it to execute the characterCounter.n file by passing it an argument: Benjamin.

  • Possible improvements: Our program is quite simple, but if you've some experience with programming, you may have noticed one point where our program may encounter an exception: we are relying on the fact that the user will give us at least one argument, and we access it directly without verifying that it's really there. So, if the user gives us no argument, we will encounter an exception. There are two ways to handle that: either we verify how many arguments the user has given, or we can also try to catch exceptions. This is something that we will explain later.

Pop quiz – basic knowledge

  1. It is possible to install haXe on:

    1. Windows

    2. MacOSX

    3. Linux

    4. Android

    5. iOS

  2. What major version do we use nowadays?

    1. haXe 1

    2. haXe 2

    3. haXe 3

    4. haXe 4

  3. The main function of a program has to be:

    1. static and named main

    2. public and named first

    3. public and static, and named first

  4. The debugging function that prints some text is named:

    1. println

    2. print

    3. trace

    4. debug

You have been reading a chapter from
haXe 2 Beginner's Guide
Published in: Jul 2011
Publisher:
ISBN-13: 9781849512565
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image