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.
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."); } }
Open a terminal; go to the directory where you saved
Main.hx
and type thehaxe
–main Main
-neko characterCounter.n
command.Type the command (this is an example with my first name):
neko characterCounter.n Benjamin
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 ofneko.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 asStringBuf
that can be used to achieve the same behavior, if you are focused on performances.You'll also notice that
String
has a variable namedlength
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 fromInt
toString
.Std.string
can be used to convert any value to aString
. This is not mandatory here, as when using the+
operator, if one of the two values is not anInt
nor aFloat
, the operator will return aString
. In this case, all operands will be considered asString
.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
It is possible to install haXe on:
Windows
MacOSX
Linux
Android
iOS
What major version do we use nowadays?
haXe 1
haXe 2
haXe 3
haXe 4
The main function of a program has to be:
static and named main
public and named first
public and static, and named first
The debugging function that prints some text is named:
println
print
trace
debug