-
Notifications
You must be signed in to change notification settings - Fork 1
types
The C~, type system knows seven type groups:
| Group | Type | Prefix |
|---|---|---|
| Numeric | Int, Float | n |
| String | String | s |
| Array | Array, QueueArray, Map | a |
| Object | Class Object | o |
| Function | Function | f |
| Delegate | Delegate | d |
| Postprocessing | Typedef | t |
Basic types are Bool, Float and Int. They can easily be identified in the syntax by values such as
| Operator | Operation | Operand 1 | Operand 2 | Return |
|---|---|---|---|---|
++ |
increment | Int |
N/A | Int |
Float |
N/A | Float |
||
-- |
decrement | Int |
N/A | Int |
Float |
N/A | Float |
||
+ |
addition | Float |
Float |
Float |
Float |
Int |
Float |
||
Int |
Float |
Float |
||
Int |
Int |
Int |
||
- |
subtraction | Float |
Float |
Float |
Float |
Int |
Float |
||
Int |
Float |
Float |
||
Int |
Int |
Int |
||
* |
multiplication | Float |
Float |
Float |
Float |
Int |
Float |
||
Int |
Float |
Float |
||
Int |
Int |
Int |
||
/ |
division | Float |
Float |
Float |
Float |
Int |
Float |
||
Int |
Float |
Float |
||
Int |
Int |
Float |
||
% |
modulo | Float |
Float |
Float |
Float |
Int |
Float |
||
Int |
Float |
Float |
||
Int |
Int |
Int |
Operator | Operation | Operand 1 | Operand 2 | Return
--- | --- | --- | --- | --- | --- | --- | --- | ---
== | equal | Float/Int | Float/Int | Bool
!= | not equal | Float/Int | Float/Int | Bool
< | less than | Float/Int | Float/Int | Bool
<= | less than or equal | Float/Int | Float/Int | Bool
> | greater than | Float/Int | Float/Int | Bool
>= | great than or equal | Float/Int | Float/Int | Bool
Operator | Operation | Operand 1 | Operand 2 | Return
--- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | ---
~ | bitwise negation | Int | N/A | Int
& | bitwise and | Int | Int | Int
| | bitwise or | Int | Int | Int
^ | bitwise xor | Int | Int | Int
<< | shift left | Int | Int | Int
>> | shift right | Int | Int | Int
>>> | unsigned shift right | Int | Int | Int
For enums:
- Enum without parameters Are always represent the same value, so
MyEnum.A == MyEnum.A. - Enum with parameters Can be compared with
a.equals(b)(which is a short forType.enumEquals()).
Dynamic: Comparison involving at least one Dynamic value is unspecifed and platform-specific.
Represents a value which can be either true or false.
Values of type Bool are a common occurrence in conditions such as if and while. The following operators accept and return Bool values:
-
&&(and) -
||(or) -
!(not)
Haxe guarantees that compound boolean expressions are evaluated from left to right and only as far as necessary at run-time. For instance, an expression like A && B will evaluate A first and evaluate B only if the evaluation of A yielded true. Likewise, the expressions A || B will not evaluate B if the evaluation of A yielded true, because the value of B is irrelevant in that case. This is important in cases such as this:
if (object != null && object.field == 1) { }Accessing object.field if object is null would lead to a run-time error, but the check for object != null guards against it.
Void denotes the absence of a type. It is used to express that something (usually a function) has no value.
Void is a special case in the type system because it is not actually a type. It is used to express the absence of a type, which applies mostly to function arguments and return types.
We have already "seen" Void in the initial "Hello World" example:
class Main {
public function Main():Void {
Debug.fTrace("Hello World");
}
}The function type will be explored in detail in the section types-function but a quick preview helps here: The type of the function main in the example above is Void->Void, which reads as "it has no arguments and returns nothing".
Haxe does not allow fields and variables of type Void and will complain if an attempt at declaring such is made:
// Arguments and variables of type Void are not allowed
var x:Void;A type in Haxe is considered nullable if
nullis a valid value for it.
It is common for programming languages to have a single, clean definition for nullability. However, Haxe has to find a compromise in this regard due to the nature of Haxe's target languages: While some of them allow and; in fact, default to null for anything, others do not even allow null for certain types. This necessitates the distinction of two types of target languages:
Static targets employ their own type system where
nullis not a valid value for basic types. This is true for the Flash, C++, Java and C# targets.
Dynamic targets are more lenient with their types and allow
nullvalues for basic types. This applies to the JavaScript, PHP, Neko and Flash 6-8 targets.
There is nothing to worry about when working with null on dynamic targets; however, static ones may require some thought. For starters, basic types are initialized to their default values.
Basic types have the following default values on static targets:
Int:0Float:NaNon Flash,0.0on other static targetsBool:false
As a consequence, the Compiler does not allow the assignment of null to a basic type on static targets. In order to achieve this, the basic type has to be wrapped as Null<T>:
Footer