Chapter 2. Basic Syntax and Branching

Basic constructs and making decisions.

In this chapter, we will learn about basic haXe constructs and pieces that make a program. We will also learn about branching (those are indeed constructs too), so that your program can make decisions and choose different paths according to conditions.

In fact, this chapter is one of the most important to get you started because it will teach you how to tell a haXe program what to do.

In this second chapter, we are going to learn quite a lot of things. The great thing is that after this chapter, you will have the knowledge to create programs that won't always do the same things. You will know about all haXe constructs too and therefore, should be able to have fun with haXe.

In this chapter, we will:

  • Learn about modules, packages, and classes
  • Learn about constants
  • Talk about binary and unary operators
  • Learn what blocks are and their particularities
  • Learn about variables and scope
  • Talk about how to access fields and call methods
  • Learn about conditional branching
  • Learn about loops
  • Learn about functions
  • Learn about exceptions and how to handle them
  • Learn about anonymous objects
  • Learn about local functions
  • Write our first complex application: a fridge management application

Don't be afraid of the list! Although it may seem quite long, you will see that it's all pretty easy to understand. This chapter is a pretty important one, so take the time to go through it. In this chapter, you will see a lot of short examples of code. You should read them carefully, and you should be able to understand them without too many explanations.

Modules, packages, and classes

If you're familiar with Object Oriented Programming (OOP) languages, then there are chances that you at least know the words "packages" and "classes"; if that's not the case, don't panic, you are going to learn all of it here.

Packages

Packages are a convenient way of splitting code into groups. Doing so, allows one to have several classes with the same name in different packages. This can be really useful, as you may, for example support two scripting languages in an application and need to write an interpreter class for each one.

Packages are represented by folders under your source directory on your filesystem. Each package has a path, which is a string obtained by joining the folders' name with dots. So, for example, if your source folder is /dev/myProject/src and you have a folder /dev/myProject/src/proj/dao, you have a package whose path is proj.da (you also have a package "proj"). There's a special package that has an empty path; it is named the top-level package and is represented by your source folder.

Each part of a package name must always begin with a lower-case character.

Note that you may have multiple source folders. Each of them should be given to the compiler with the -cp flag, also, the current working directory is automatically added to the list of source folders.

If you have files colliding in different source folders, the ones from the latest included source folder will take precedence over (hide) the others. This can be interesting to know sometimes, as you may have the feeling that your changes to a file are not taken into account.

Modules

Modules are stored in packages. They are represented by files with the .hx extension in your filesystem. They indeed are the files that will contain your haXe code. Modules follow the same naming scheme as packages, except that the first character of their name must be in uppercase. That means, if you have a file named Food.hx in /src/bo/, then the full path of your module will be bo.Food.

Modules will contain all the types, which you will declare. That can be classes, typedefs, interfaces, and enums. We will only talk about classes at the moment.

Modules can contain a main type that has the same name as the module. Also, note that if a module is in a package that's not the top-level package, it must begin by the line package package.path.

Classes

Classes are the base of Object Oriented Programming, but since this is not a book about OOP, we'll just have a quick reminder: classes are a bit like a blueprint. When you create an object from a class (what we call "instantiate"), you make sure that this object will have all the fields (such as variables, functions, and properties) declared in the class (but not those that are static). The object will also be of the type declared by the class.

The first character of a class' name must be uppercase.

Accessing a class

A class that is a module's main type can be accessed by using its complete path (that is, its package's path concatenated with a dot and its name) or by using its name, if using it from the same package.

If you want to access a class that is not the module's main type, you must use the complete path to the module concatenated with a dot and the class' name, or, if you're in the same module, you can simply use the class name.

Note that the two precedent paragraphs are true for all kinds of types in haXe.

The following is a commented example:

package food; //We are in the food package
class Meat //Full path to access this class : food.Meat
{
public var name : String; //All instances of Meat will have a variable name of type String
public var size : Int;
}
class Fruit //Full path to access this class : food.Meat.Fruit
{
public var name : String;
public var vitamins : String;
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.162.49