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.
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:
true
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.
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.
Floats can be written using two different notations; let's take a look at both.
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.
Strings can be written by simply enclosing them between two double quotes (") or between two single quotes ('). So, the following are two examples:
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:
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.
You can also write regular expressions by beginning them with ~/
, so for example:
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 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.
18.225.95.245