Every language has a way of storing data. Data is of 2 types - variables and constants.
Constants are values such as 3, 1000, hello, etc. A variable is a named location in the memory that can be used to hold changeable (variable) data.

Data Types In haXe

All data, whether constant or variable has a type ( or data type ). Haxe has the following data types -
  • Integer ( Int )

These include integer numbers such as 50,1235,etc. They can also be hexadecimal numbers by prefixing them with 0x like 0xFA.
  • Float

These include decimal point numbers like 2.012, 1340.645, etc. They can also be represented by scientific notation, i.e. 10 to the power of something, like 5e10 where e is 10. So, this number is 5 X 10^10
  • Boolean ( Bool )

Boolean data can have only two values - true and false. It is another way of saying 1 and 0.
  • String

Strings represent a letter or a group of letters like h, hello, etc. They are always enclosed in double quotes.
  • Void

The Void type is a placeholder for no type or value . Very often, a type is required within areas of your code that allow the compiler and virtual machine to understand the flow of data in your program. Using the Void type tells the compiler that no data is to be expected at a particular juncture, so no type checking is required. The usual location for such a type is as the return value. Basically, the Void data type is a way of saying that there is meant to be no data.
  • Dynamic

This is one of the coolest data types of haXe. The Dynamic type means that the variable can be of any type. Basically, the compiler does not provide type checking for that variable. So, a variable of type Dynamic can have an integer value and then can later be given a string value. This is a very useful type and I have used this extensively in my games.
  • Unknown

The Unknown type represents type - inferred variables that have yet to be assigned a type. Once a value is assigned to a variable of type Unknown, its type will be exchanged to match that of the passed data. More on this when we discuss type-inference.

Static and Dynamic Typing

haXe is a statically typed language. This means that, when you declare variables, the compiler needs to know what type of data will be assigned to the variables so that it can deal with them efficiently. But, one of the strengths of haXe is to turn itself into a dynamically typed one where the compiler does not check the type. This is done by using the Dynamic type among other things. This allows you to program with static typing but, at the same time you can program with dynamic typing when needed. This is a huge improvement from totally statically typed languages like C++. It also promotes better programming practices ( unlike dynamically typed languages like Javascript ) by using static typing.

The null Value

The null value can be represented using the null keyword. In haXe, null is of type Unknown<0>( array of unknown type), which means that the compiler doesn't know what its type is.
null is usually used to check if a variable has some undefined value or not. This can be done by checking it against the null value.

Variable Declaration

Declaration is the process of telling the compiler that you are creating a variable of a specific type. The syntax is -
[access] [static] [inline] var variableName[:dataType] [ = someValue];
We will not be concerning ourselves with access, static and inline specifiers. These will be seen when we learn OOP.
Every declaration has the keyword var, which tells the compiler that we are declaring a variable of name variableName of type dataType with an initial value someValue. The data type is always preceded by a colon. The Data type and initial value are optional in most cases.
Here is an example of declaring a variable a of type integer with initial value 5.

var a:Int = 5;
 

Declaring variables in classes


As you know, classes can have variables in them. These are global variables as they are available to all functions of the class. The syntax is almost the same. The only difference is that a variable cannot be given an initial value ( except when it is static ). It can only be given an initial value in a function. Since haXe is statically typed, the type of the variable must be specified.
See this example -
class Test
{
 
  var d:String;
 
  function assign()
  {
    d = "testing";
  }
 
}
Here we declare a variable of type String in class Test. We need to specify the type and can only give it a value in a function.

Declaring variables in a function


Declaring in a function is the same except that access and static keywords are not applicable. They are applicable only in a class. One important thing is that unlike classes, the initial value or the data type or both can be omitted when declaring a variable in a function. Let us rewrite the above example and declare the variable d in the function assign -
class Test
{
  function assign()
  {
    var d:String = "testing";
  }
 
}
It could also be -
class Test
{
 
  function assign()
  {
    var d = "testing";
  }
 
}
Or this -
class Test
{
 
  function assign()
  {
    var d:String;
    d = "testing";
  }
 
}
Or this -
class Test
{
 
  function assign()
  {
    var d;
    d = "testing";
  }
 
}
Notice that although all these are different, in their syntax they are equivalent.

Type Inference


The haXe compiler is very smart. When you declare a variable you need not always (provided you will assign the variable a value in the same line or later) specify a data type for that variable. When you assign a value to it later, the haXe compiler can infer the type from the value that you are giving it. This is called Type Inference. In the last example of the section on variables in a function, we used type inference. If you look at it again, d is not given any data type when declared. It is only given one in the next line and only there is its type inferred.
The data types of variables declared in classes cannot be type inferred as they cannot be assigned a value in the class. Hence, we need to provide the data type.

Note - You might be wondering what is the type of a variable before the type is inferred. It is of type Unknown, which we mentioned earlier. Do you remember it ? Unknown is just a type that is given to a variable before it is inferred. If the type is provided by you, then it does not become of type Unknown.