Time for action – Writing a Hello World
Let's create an application that will simply display the "Hello World" message. We will use only cross-platform code and will compile it to neko to begin.
Copy the following code in a file named
Main.hx
class Main { public static function main() { trace("Hello World"); } }
Tip
Downloading the example code for this book
You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.
Open a terminal; go to the directory where you saved
Main.hx
and type thehaxe
–main Main
–neko helloworld.n
command.Type this command:
neko helloworld.n
.You will see the following output:
Main.hx:5:Hello World
What just happened?
As you've guessed, the program just wrote "Main.hx:5:Hello World" in our terminal and ended.
The code: In our code we declare a class named
Main
, which has a public and static functionmain
. The methodtrace
is a method that allows one to debug an application by printing a message along with the file's name and line number of the call. If you don't really understand what classes are, don't worry, we are going to explain it to you soon.The compilation: The command in the second step compiles the haXe code to neko. Notice that the
–main
switch that's followed by the name of the class containing the function has to be executed when launching the program. This function has to be a static function namedmain
that takes no parameters.Running the neko code: In the third step, we invoke the neko VM and tell it to execute the
helloworld.n
file.