Finally, lets start coding. This is where the fun begins.

haXe Syntax

haXe syntax is very similar to Javascript and actionscript. All statements end with a semicolon. But unlike javascript and actionscript, haXe is statically typed ( we will see what that means later).

Lets Get Down to the Code

Lets start our first code -
class Main
  public static function main()
    trace("Hello World");

Copy this code into a text editor and save it as Main.hx.

Now what is this? This program just prints Hello World in the command prompt.
OK, fine. But, what platform does this code target? Good question.

We have not defined that yet. We define the target by creating an hxml file.
Here is the code -

-main Main
-neko hello.n
Save this as compile.hxml.

This build file tells the haxe compiler to use the static function main from the Main class as the main entry point and compile it for the Neko target ( remember Neko? I have explained it a little bit earlier).

Now we need to compile this. To do that follow the steps below -

  • Place these files in the same folder
  • Open the command line
  • In the command line navigate to the folder in which Main.hx and compile.hxml are placed ( To do this in Windows, go to the cmd prompt, type cd (path) , where (path) is the path of the folder)
  • Type haxe compile.hxml

Cool ! If you check the folder there will now be a file hello.n generated. This is a neko executable file, which can be executed on a computer with Neko installed.
To execute this file type neko hello.n.

You get a Hello World message displayed. Awesome!
Lets now understand how it happened.

Performing the Code Autopsy

Lets dissect the code.

A haXe file has the extension .hx.
Everything in haXe is made up of something called classes. Classes are like separate entities which can be combined together to create an application. They contain data and functions that the program requires to execute.
Most of the time, only one class can be made in a hx file and the name of the hx file should be the same as that of the class it contains.

In our example, the name of the class ( as well as the hx file was Main).

It is important to remember that classes are found not only in haXe. Classes are used in many languages, like Java, C++, etc. The technique ( or paradigm ) of using classes as the basis of your application and allowing them to interact in different ways ( by using inheritance, polymorphism, encapsulation, etc. ) is called Object Oriented Programming (OOP) .

Now, OOP is a complex computer science topic and it needs a separate chapter to discuss ( a full book in fact, but we don't need that much ) . Don't worry if you didn't understand classes, I will be explaning them in a later section.

Now, coming to the main function. Our main application class ( Main in this case ) should have a main function to execute. It contains the following code:
trace("Hello World");
The trace function is a standard haXe function which allows you to output something to an output device. We want it to display Hello World on the command prompt screen ( which is the default output device). So, we pass the value Hello World to it.

We then define the hxml file to target the neko platform.

It must be noted that the main function has to be preceded by the word static. The meaning of the static keyword will be explained in the OOP section.

We finally wrote our first code !

You can now go to the next lesson.