Constants and its types

There are six types of constants in haXe. We will now take a look at all of them. Some of them are composed of several kinds of them.

Booleans

The first type of constants is one that is spread most across programming languages: Booleans. For your information, in haXe Booleans have the type Bool. The following are two expressions, which are Boolean constants:

  1. true
  2. false

That's both easy and important. By the way, in haXe Bool is indeed an Enum (if you don't know what this is, don't worry, we will learn about it later). This Enum has two values, and true and false are just reference to these values.

Booleans are generally used to represent the truthiness of things. For example, when one is presented with a checkbox in an interface, the "checked" property of the checkbox can be of type Bool.

Integers

This one is easy; you can use integers constants by simply writing their values, for example:

  • 1234
  • -456

You can also use the hexadecimal notation by starting your value with 0x:

  • 0xF
  • 0xF0

The first example has a value of 15 and the second one has a value of 240.

Note that in haXe, integers have the type Int.

Floats

Floats can be written using two different notations; let's take a look at both.

Base 10

You can write floats values directly in base 10, as follows:

  • 0.72
  • -0.45
  • .50
  • -.23

As you can see, if the integral part of your float is 0, you can simply omit it.

Scientific notation

Floats can be written using the scientific notation too: this should be pretty familiar to you:

  • 1e2
  • 1e-3
  • -2e4
  • -2e-4

The first value here is 100 and, before you ask, yes it's a float here! The second one is 0.001 and as you've guessed, the third one is -20000 and the fourth one is -0.002.

Strings

Strings can be written by simply enclosing them between two double quotes (") or between two single quotes ('). So, the following are two examples:

  1. "Hello World!"
  2. 'Hello Reader!'

Those are simple strings, but you may also need to separate some special characters; this can be done using the back-slash () character, as follows:

  1. "Hello "somebody""somebody""
  2. 'This one has a line-break here'

Also, note that in haXe, strings can span across several lines, so you could also write:

"This one has a line-break
here"

This is something which does not necessary exist in all languages, but it can be really useful.

Regular expressions

You can also write regular expressions by beginning them with ~/, so for example:

  • ~/[a-ZA-Z]+/

Note that regular expressions in haXe have the type EReg.

The following is an example of how you can test whether a regular expression matches a String:

class TesthaXe
{
public static function main(): Void
{
var e = ~/^Hello.*/; //Creates an EReg matching any string beginning with Hello. var e = simply declares the variable and assign the value on the right to it.
if(e.match("Hello Benjamin"))
{
neko.Lib.println("Matches");
} else
{
neko.Lib.println("Does not match");
}
}
}

This code will print Matches. The following is the same example written in a slightly different way. Note how the regular expression is created:

class TesthaXe
{
public static function main(): Void
{
var e = new EReg("^Hello.*",""); //Creates an EReg matching any string beginning with Hello
if(e.match("Hello Benjamin"))
{
neko.Lib.println("Matches");
} else
{
neko.Lib.println("Does not match");
}
}
}

This code does exactly the same thing and the created regular expression is exactly the same. Note that the EReg's constructor takes two strings as its parameters: the first one is the pattern of the regular expression, while the other one contains the options of the regular expression. Also, note that such options can be passed when creating an EReg using the first notation by passing them after the second slash. For example: ~/[aZ]/g.

The null value

The null value can be represented using the null keyword. Please note that in haXe, null is of type Unknown<0>, which means that the compiler doesn't know what its type is. That may seem a bit strange but it does make sense; indeed, because of that you will be able to use the null value in place of any type.

Also, because null can be used for any type, you may want, particularly when converting some user input or when writing libraries, to test against a null value to avoid runtime errors.

The following is an example:

class TesthaXe
{
public static function main(): Void
{
var l = new List<String>();
printLoweredString(l.first());
}
public static function printLoweredString(s : String)
{
neko.Lib.println(s.toLowerCase());
}
}

As the list is empty, the call to first will return a null value. In printLoweredString, the call to toLowerCase on a null value cannot succeed and will result in an exception being thrown at runtime. You can avoid this, by using the following example:

class TesthaXe
{
public static function main(): Void
{
var l = new List<String>();
if(l.first() != null)
{
printLoweredString(l.first());
}
}
public static function printLoweredString(s : String)
{
neko.Lib.println(s.toLowerCase());
}
}

Depending on how you want your code to behave, you may want to write the test in the printLoweredString function.

Also, note that using the first function twice certainly does not optimize the process; it would certainly be better to store the result in a variable.

Flash9 and above

In Flash9 and above, it's not possible to assign null to variables that are of a type considered as basic. Those types include, for example, Int and String.

If you want to be able to assign null to such variables, you have to declare them using the Null type, for example:

var e : Null<Int>;
..................Content has been hidden....................

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