Haxe: Difference between revisions
describing anonymous functions in Haxe |
Undid revision 1258208252 by Kongpc14567 (talk) |
||
(13 intermediate revisions by 9 users not shown) | |||
Line 5: | Line 5: | ||
| logo = Haxe logo.svg |
| logo = Haxe logo.svg |
||
| logo size = x64px |
| logo size = x64px |
||
| paradigm = [[Programming paradigm#Multi-paradigm|Multi-paradigm]] |
| paradigm = [[Programming paradigm#Multi-paradigm|Multi-paradigm]]: [[Object-oriented programming|object-oriented]], [[Functional programming|functional]], [[Generic programming|generic]] |
||
| family = [[ECMAScript]] |
|||
| developer = Haxe Foundation |
| developer = Haxe Foundation |
||
| released = {{Start date and age|2005|11|14}} |
| released = {{Start date and age|2005|11|14}} |
||
| latest release version = {{wikidata|property|edit|reference|P348}} |
| latest release version = {{wikidata|property|edit|reference|P348}} |
||
| latest release date = {{start date and age|{{wikidata|qualifier|P348|P577}}}} |
| latest release date = {{start date and age|{{wikidata|qualifier|P348|P577}}}} |
||
| typing = [[Type system| |
| typing = [[Type system|static]], [[Dynamic typing|dynamic]] via annotations, [[Nominal type system|nominal]] |
||
| scope = [[Scope (computer science)|lexical]] |
|||
⚫ | |||
⚫ | |||
⚫ | |||
| operating system = [[Android (operating system)|Android]], [[iOS]]; [[Linux]], [[macOS]], [[Microsoft Windows|Windows]] |
| operating system = [[Android (operating system)|Android]], [[iOS]]; [[Linux]], [[macOS]], [[Microsoft Windows|Windows]] |
||
| license = [[GNU General Public License|GPL]] 2.0, library: [[MIT License|MIT]] |
| license = [[GNU General Public License|GPL]] 2.0, library: [[MIT License|MIT]] |
||
Line 17: | Line 20: | ||
| website = {{Official URL}} |
| website = {{Official URL}} |
||
| influenced by = [[ECMAScript]], [[JavaScript]], [[ActionScript]], [[OCaml]], [[Java (programming language)|Java]], [[C++]], [[PHP]], [[C Sharp (programming language)|C#]], [[Python (programming language)|Python]], [[Lua (programming language)|Lua]], [[NekoVM]] |
| influenced by = [[ECMAScript]], [[JavaScript]], [[ActionScript]], [[OCaml]], [[Java (programming language)|Java]], [[C++]], [[PHP]], [[C Sharp (programming language)|C#]], [[Python (programming language)|Python]], [[Lua (programming language)|Lua]], [[NekoVM]] |
||
⚫ | |||
}} |
}} |
||
'''Haxe''' is a high-level [[cross-platform]] [[programming language]] and [[compiler]] that can produce applications and source code |
'''Haxe''' is a [[High-level programming language|high-level]] [[cross-platform]] [[programming language]] and [[compiler]] that can produce applications and [[source code]] for many different [[computing platform]]s from one code-base. It is [[free and open-source software]], released under an [[MIT License]].<ref>{{Cite web |title=Open Source licence Haxe |url=https://haxe.org/foundation/open-source.html}}</ref> The compiler, written in [[OCaml]], is released under the [[GNU General Public License]] (GPL) version 2. |
||
Haxe includes a set of features and a standard library<ref name="hfeatures">[https://haxe.org/documentation/introduction/stdlib-introduction.html Introduction to the Haxe Standard Library], Haxe Docs</ref> supported [[Cross-platform software|across all platforms]], |
Haxe includes a set of features and a [[standard library]]<ref name="hfeatures">[https://haxe.org/documentation/introduction/stdlib-introduction.html Introduction to the Haxe Standard Library], Haxe Docs</ref> supported [[Cross-platform software|across all platforms]], including [[Integer (computer science)|numeric data types]], [[String (computer science)|strings]], [[Array data type|arrays]], [[Associative array|maps]], [[Binary file|binary]], [[reflective programming]], maths, Hypertext Transfer Protocol ([[HTTP]]), file system and common [[file format]]s. Haxe also includes platform-specific application programming interfaces ([[API]]s) for each compiler target.<ref>[https://haxe.org/documentation/introduction/stdlib-introduction.html#target-apis Target Specific APIs, Introduction to the Haxe Standard Library], Haxe Docs</ref> ''Kha'', ''[[OpenFL]]'', and ''Heaps.io'' are popular Haxe frameworks that enable creating multi-platform content from one codebase.<ref name="gs_adobe">{{cite web |last1=Doucet |first1=Lars |date=2014-06-24 |url=http://www.gamasutra.com/blogs/LarsDoucet/20140624/219674/Dear_Adobe_Support_Haxe_save_your_Tools.php |title=Dear Adobe: Support Haxe, save your Tools |website=Gamasutra}}</ref> |
||
Haxe originated<ref name="haxe-interview">{{cite |
Haxe originated with the idea of supporting [[client-side]] and [[server-side]] programming in one language, and simplifying the communication logic between them.<ref name="haxe-interview">{{cite news |url=http://ncannasse.fr/blog/haxe_interview |title=Haxe Interview |publisher=Io Programmo |date=2009-04-01 |pages=1–6 |access-date=2015-08-06 |archive-date=2015-12-08 |archive-url=https://web.archive.org/web/20151208134720/http://ncannasse.fr/blog/haxe_interview |url-status=dead }}</ref> Code written in Haxe can be [[source-to-source compiler|compiled]] into [[JavaScript]], [[C++]], [[Java (programming language)|Java]], [[Java (programming language)|JVM]], [[PHP]], [[C Sharp (programming language)|C#]], [[Python (programming language)|Python]], [[Lua (programming language)|Lua]]<ref>{{Cite web |url=https://haxe.org/blog/hello-lua |title=Hello Lua! |website=Haxe.org}}</ref> and [[Node.js]].<ref>{{Cite web|url=https://lib.haxe.org/p/hxnodejs |title=hxnodejs}}</ref> Haxe can also directly compile [[SWF]], HashLink,<!-- Linking this term makes a WP:SELFLINK. Do not link until dedicated section or article exists. --> and [[NekoVM]] [[bytecode]] and also runs in interpreted mode.<ref name="compiler-targets">{{Cite web |title=Compiler Targets |url=https://haxe.org/documentation/introduction/compiler-targets.html |website=Haxe |access-date=2021-11-05}}</ref> |
||
Haxe supports externs (definition files) that can contain type information of |
Haxe supports externs (definition files) that can contain [[data type]] information of extant [[Library (computing)|libraries]] to describe target-specific interaction in a type-safe manner, like [[C++]] [[header files]] can describe the structure of existing [[object file]]s. This enables to use the values defined in the files as if they were statically typed Haxe entities. Beside externs, other solutions exist to access each platform's native capabilities. |
||
Many popular [[Integrated development environment|IDE]]s and [[source code editor]]s have |
Many popular [[Integrated development environment|IDE]]s and [[source code editor]]s have support available for Haxe development.<ref>[https://haxe.org/documentation/introduction/editors-and-ides.html List of IDEs supporting Haxe], Haxe Foundation</ref> No particular development environment or tool set is officially recommended by the Haxe Foundation, although [[VS Code]], [[IntelliJ IDEA]] and HaxeDevelop have the most support for Haxe development. The core functionalities of [[syntax highlighting]], [[code completion]], [[refactoring]], [[debugging]], etc. are available to various degrees. |
||
==History== |
==History== |
||
Line 37: | Line 39: | ||
==Compiler== |
==Compiler== |
||
The Haxe language can compile into [[bytecode]] that can be executed directly by the [[virtual machine]]s it targets. It can compile to [[source code]] in [[C++]], [[JavaScript]], [[PHP]], [[C Sharp (programming language)|C#]], [[Java (programming language)|Java]], [[Python (programming language)|Python]], and [[Lua (programming language)|Lua]]. Haxe also has an interpreter called ''eval''.<ref>{{Cite web|title=Eval - The new Haxe macro interpreter|url=https://haxe.org/blog/eval/}}</ref> This same interpreter is also used compile-time to run macros, which allow modification of the [[abstract syntax tree]] (AST). |
The Haxe language can compile into [[bytecode]] that can be executed directly by the [[virtual machine]]s it targets. It can compile to [[source code]] in [[C++]], [[JavaScript]], [[PHP]], [[C Sharp (programming language)|C#]], [[Java (programming language)|Java]], [[Python (programming language)|Python]], and [[Lua (programming language)|Lua]]. Haxe also has an interpreter called ''eval''.<ref>{{Cite web |title=Eval - The new Haxe macro interpreter |url=https://haxe.org/blog/eval/}}</ref> This same interpreter is also used compile-time to run macros, which allow modification of the [[abstract syntax tree]] (AST). |
||
This strategy of compiling to multiple source code languages is inspired by the [[write once, run anywhere]] paradigm. It also allows the programmer to choose the best platform for the job. Typical Haxe programs run identically on all platforms, but developers can specify platform-specific code and use [[conditional compilation]] to prevent it from compiling on other platforms. |
This strategy of compiling to multiple source code languages is inspired by the [[write once, run anywhere]] paradigm. It also allows the programmer to choose the best platform for the job. Typical Haxe programs run identically on all platforms, but developers can specify platform-specific code and use [[conditional compilation]] to prevent it from compiling on other platforms. |
||
Line 50: | Line 52: | ||
The following table documents platform and language support in Haxe. The Haxe language allows developers to gain access to many platform features, but Haxe is not a full featured engine, they might need frameworks that enable create content for certain platforms. |
The following table documents platform and language support in Haxe. The Haxe language allows developers to gain access to many platform features, but Haxe is not a full featured engine, they might need frameworks that enable create content for certain platforms. |
||
{| class="wikitable sortable" |
{| class="wikitable sortable" |
||
|+ Haxe |
|+ Haxe compiler targets<ref name="compiler-targets" /> |
||
|- |
|- |
||
! scope="col" | Name |
! scope="col" | Name |
||
Line 66: | Line 68: | ||
| 2005 (alpha) |
| 2005 (alpha) |
||
|- |
|- |
||
! scope="row" | |
! scope="row" |Flash/SWF |
||
| 3 |
| 3 |
||
| byte code |
| byte code |
||
Line 104: | Line 106: | ||
| 3 |
| 3 |
||
| source |
| source |
||
| [[C Sharp (programming language)|C#]]: [[.NET |
| [[C Sharp (programming language)|C#]]: [[.NET]] framework, [[.NET Core]], [[Mono (software)|Mono]] |
||
| Server, desktop, mobile |
| Server, desktop, mobile |
||
| 2012 (2.10) |
| 2012 (2.10) |
||
Line 151: | Line 153: | ||
|} |
|} |
||
=== Advantages |
=== Advantages === |
||
* Ability to target multiple platforms and devices using the same language |
* Ability to target multiple platforms and devices using the same language |
||
* Ability to use strictly-typed code |
* Ability to use strictly-typed code |
||
Line 159: | Line 161: | ||
==Language== |
==Language== |
||
Haxe is a general-purpose language supporting [[object-oriented programming]], [[generic programming]], and various [[functional programming]] constructs. Features such as [[iteration]]s, [[Exception handling|exceptions]], and [[ |
Haxe is a [[general-purpose programming language]] supporting [[object-oriented programming]], [[generic programming]], and various [[functional programming]] constructs. Features such as [[iteration]]s, [[Exception handling|exceptions]], and [[reflective programming]] (code reflection) are also built-in functions of the language and libraries. Unusual among programming languages, Haxe contains a [[type system]] which is both [[Strong and weak typing|strong]] and [[Type system#Dynamic typing|dynamic]]. The compiler will check types implicitly via [[type inference]] and give compile-time errors, but it also enables programs to bypass type-checking and rely on a target platform's dynamic type-handling. All of the native target APIs can be used. |
||
===Type system=== |
===Type system=== |
||
Haxe has a sophisticated and flexible type system. The type kinds it offers are classes, interfaces, function-method types, anonymous types, [[algebraic data type]]s (called ''enum'' in Haxe), and abstract types. [[Parametric polymorphism]] is possible with classes, algebraic types and function types, giving the language support for [[generic programming]] based on type erasure. This includes support for [[Variance (computer science)|variance]] in [[polymorphic function]]s, although not in [[type constructor]]s. |
Haxe has a sophisticated and flexible type system. The type kinds it offers are [[Class (computer programming)|classes]], interfaces, function-method types, anonymous types, [[algebraic data type]]s (called ''enum'' in Haxe), and abstract types. [[Parametric polymorphism]] is possible with classes, algebraic types and function types, giving the language support for [[generic programming]] based on type erasure. This includes support for [[Variance (computer science)|variance]] in [[polymorphic function]]s, although not in [[type constructor]]s. |
||
The [[type system]] is [[Static typing|static]] unless annotations for [[dynamic typing]] are present, for use with targets that support them. [[Type checking]] follows [[Nominal type system|nominal typing]] with the exception of [[anonymous type]]s where [[structural typing]] is used instead. Finally, [[type inference]] is supported, allowing for variable declarations without [[type annotation]]s. |
The [[type system]] is [[Static typing|static]] unless annotations for [[dynamic typing]] are present, for use with targets that support them. [[Type checking]] follows [[Nominal type system|nominal typing]] with the exception of [[anonymous type]]s where [[structural typing]] is used instead. Finally, [[type inference]] is supported, allowing for variable declarations without [[type annotation]]s. |
||
Line 284: | Line 286: | ||
===Anonymous functions=== |
===Anonymous functions=== |
||
{{Excerpt| |
{{Excerpt|Examples of anonymous functions|Haxe|subsections=yes}} |
||
===Abstract types=== |
===Abstract types=== |
||
A relatively new addition to the Haxe type system is a concept termed ''abstract types''. As used in Haxe, this refers to something different from a conventional [[abstract type]]. They are used to make conversions between types implicit, allowing reuse of existing types for specific purposes, like implementing types for units of measurement. This greatly reduces the risk of mixing up values of the same underlying type, but with different meanings (e.g., miles vs. km). |
A relatively new addition to the Haxe type system is a concept termed ''abstract types''. As used in Haxe, this refers to something different from a conventional [[abstract type]]. They are used to make conversions between types implicit, allowing reuse of existing types for specific purposes, like implementing types for units of measurement. This greatly reduces the risk of mixing up values of the same underlying type, but with different meanings (e.g., miles vs. km). |
||
Line 343: | Line 345: | ||
==={{visible anchor|Compiler|Compiler implementation and performance}}=== |
==={{visible anchor|Compiler|Compiler implementation and performance}}=== |
||
The Haxe compiler is divided into one frontend and multiple backends. The frontend creates an [[abstract syntax tree]] (AST) from the source code, and performs type checking, [[macro expansion]], and optimization on the AST. The various backends [[Source-to-source compiler|translate]] the processed AST into source code or generate [[bytecode]], depending on their target. |
The Haxe compiler is divided into one frontend and multiple backends. The frontend creates an [[abstract syntax tree]] (AST) from the source code, and performs type checking, [[Macro (computer science)|macro expansion]], and optimization on the AST. The various backends [[Source-to-source compiler|translate]] the processed AST into source code or generate [[bytecode]], depending on their target. |
||
The compiler is written in [[OCaml]]. It can be run in server-mode to provide code completion for [[integrated development environment]]s (IDEs) and maintain a cache, to further speed compiling.<ref>Server mode command-line: <code>haxe --wait [host:]port</code></ref> |
The compiler is written in [[OCaml]]. It can be run in server-mode to provide code completion for [[integrated development environment]]s (IDEs) and maintain a cache, to further speed compiling.<ref>Server mode command-line: <code>haxe --wait [host:]port</code></ref> |
||
Line 349: | Line 351: | ||
==See also== |
==See also== |
||
{{Portal|Free and open-source software}} |
{{Portal|Free and open-source software}} |
||
* [[Comparison of IDE choices for Haxe programmers]] |
|||
* [[Dart (programming language)]] |
* [[Dart (programming language)]] |
||
* [[Nim (programming language)]] |
* [[Nim (programming language)]] |
||
Line 369: | Line 370: | ||
{{Adobe Flash}} |
{{Adobe Flash}} |
||
{{JavaScript}} |
{{JavaScript}} |
||
{{ML programming}} |
|||
[[Category:High-level programming languages]] |
|||
[[Category:Functional languages]] |
[[Category:Functional languages]] |
||
[[Category:Multi-paradigm programming languages]] |
[[Category:Multi-paradigm programming languages]] |
||
Line 380: | Line 383: | ||
[[Category:Source-to-source compilers]] |
[[Category:Source-to-source compilers]] |
||
[[Category:Statically typed programming languages]] |
[[Category:Statically typed programming languages]] |
||
<!-- Hidden categories below --> |
|||
[[Category:Articles with example JavaScript code]] |
Latest revision as of 07:06, 23 November 2024
Paradigm | Multi-paradigm: object-oriented, functional, generic |
---|---|
Family | ECMAScript |
Developer | Haxe Foundation |
First appeared | November 14, 2005 |
Stable release | 4.3.6[1]
/ 7 August 2024 |
Typing discipline | static, dynamic via annotations, nominal |
Scope | lexical |
Implementation language | OCaml |
Platform | IA-32, x86-64, AArch64, armel, armhf, MIPS, MIPS64el, MIPSel, ppc64el, RISC-V, s390x |
OS | Android, iOS; Linux, macOS, Windows |
License | GPL 2.0, library: MIT |
Filename extensions | .hx, .hxml |
Website | haxe |
Influenced by | |
ECMAScript, JavaScript, ActionScript, OCaml, Java, C++, PHP, C#, Python, Lua, NekoVM |
Haxe is a high-level cross-platform programming language and compiler that can produce applications and source code for many different computing platforms from one code-base. It is free and open-source software, released under an MIT License.[2] The compiler, written in OCaml, is released under the GNU General Public License (GPL) version 2.
Haxe includes a set of features and a standard library[3] supported across all platforms, including numeric data types, strings, arrays, maps, binary, reflective programming, maths, Hypertext Transfer Protocol (HTTP), file system and common file formats. Haxe also includes platform-specific application programming interfaces (APIs) for each compiler target.[4] Kha, OpenFL, and Heaps.io are popular Haxe frameworks that enable creating multi-platform content from one codebase.[5]
Haxe originated with the idea of supporting client-side and server-side programming in one language, and simplifying the communication logic between them.[6] Code written in Haxe can be compiled into JavaScript, C++, Java, JVM, PHP, C#, Python, Lua[7] and Node.js.[8] Haxe can also directly compile SWF, HashLink, and NekoVM bytecode and also runs in interpreted mode.[9]
Haxe supports externs (definition files) that can contain data type information of extant libraries to describe target-specific interaction in a type-safe manner, like C++ header files can describe the structure of existing object files. This enables to use the values defined in the files as if they were statically typed Haxe entities. Beside externs, other solutions exist to access each platform's native capabilities.
Many popular IDEs and source code editors have support available for Haxe development.[10] No particular development environment or tool set is officially recommended by the Haxe Foundation, although VS Code, IntelliJ IDEA and HaxeDevelop have the most support for Haxe development. The core functionalities of syntax highlighting, code completion, refactoring, debugging, etc. are available to various degrees.
History
[edit]Development of Haxe began in October 2005. The first alpha version was released on November 14, 2005.[11][12] Haxe 1.0 was released in April 2006, with support for Adobe Flash, JavaScript, and NekoVM programs. Support for PHP was added in 2008, and C++ was added in 2009. More platforms such as C# and Java were added with a compiler overhaul in 2012.
Haxe was developed by Nicolas Cannasse and other contributors, and was originally named haXe[13] because it was short, simple, and "has an X inside", which the author asserts humorously is needed to make any new technology a success.[14]
Haxe is the successor to the open-source ActionScript 2 compiler MTASC,[15] also built by Nicolas Cannasse and is released under the GNU General Public License version 2 or later.
Compiler
[edit]The Haxe language can compile into bytecode that can be executed directly by the virtual machines it targets. It can compile to source code in C++, JavaScript, PHP, C#, Java, Python, and Lua. Haxe also has an interpreter called eval.[16] This same interpreter is also used compile-time to run macros, which allow modification of the abstract syntax tree (AST).
This strategy of compiling to multiple source code languages is inspired by the write once, run anywhere paradigm. It also allows the programmer to choose the best platform for the job. Typical Haxe programs run identically on all platforms, but developers can specify platform-specific code and use conditional compilation to prevent it from compiling on other platforms.
The Haxe compiler is an optimizing compiler, and uses field and function inlining, tail recursion elimination, constant folding, loop unrolling and dead code elimination (DCE) to optimize the run-time performance of compiled programs.[17] The Haxe compiler offers opt-in null-safety, it checks compile-time for nullable values.
Targets
[edit]In Haxe, supported platforms are known as "targets", which consist of the following modules:
- The compiler-backends that are responsible for generating the respective code.
- The run-time specific APIs that go beyond the core language support (platform-targets).
The following table documents platform and language support in Haxe. The Haxe language allows developers to gain access to many platform features, but Haxe is not a full featured engine, they might need frameworks that enable create content for certain platforms.
Name | Tier | Output | Platform | Use | Since Haxe version |
---|---|---|---|---|---|
Neko | 3 | byte code | NekoVM | Server, desktop, CLI | 2005 (alpha) |
Flash/SWF | 3 | byte code | Flash: AVM2, Flash Player 9+, AIR | Desktop, browser, server | 2005 (alpha) |
JavaScript | 1 | source | JavaScript: HTML5, NodeJS, PhoneGap | Server, desktop, browser, mobile | 2006 (beta) |
ActionScript | — | source | ActionScript 3: AIR, Flex, Royale | Server, desktop, browser, mobile | 2007 (1.12), removed since 2019 (4.0) |
PHP | 1 | source | PHP: Zend Engine | Server | 2008 (2.0) PHP 5; PHP 7 since 2019 (4.0) |
C++ | 2 | source | C++: Windows, Linux, macOS, Android, iOS, Palm, WebOS | Server, desktop, Mobile, CLI, game consoles | 2009 (2.04); cppia added in 2014 (3.2) |
C# | 3 | source | C#: .NET framework, .NET Core, Mono | Server, desktop, mobile | 2012 (2.10) |
Java | 3 | source | Java: Java OpenJDK | Server, desktop | 2012 (2.10) |
Python | 3 | source | Python | CLI, web, desktop | 2014 (3.2) |
Lua | 2 | source | Lua | CLI, web, desktop, mobile | 2016 (3.3) |
HashLink | 1 | byte code | HashLink VM or HL/C (compile to C file) | Server, desktop, mobile, game consoles (C export) | 2016 (3.4) |
JVM | 1 | bytecode | JVM: HotSpot, OpenJ9 | Server, desktop | 2019 (4.0) |
Eval | 1 | interpreter | Haxe interpreter | Prototyping, scripting | 2019 (4.0) |
Advantages
[edit]- Ability to target multiple platforms and devices using the same language
- Ability to use strictly-typed code
- Ability to use macros (syntax transformation) which can be done with Haxe language[18]
- Added language features such as extension methods and functional programming
- The run-time performance of Haxe programs is at comparable speed to handwritten sources.[19]
Language
[edit]Haxe is a general-purpose programming language supporting object-oriented programming, generic programming, and various functional programming constructs. Features such as iterations, exceptions, and reflective programming (code reflection) are also built-in functions of the language and libraries. Unusual among programming languages, Haxe contains a type system which is both strong and dynamic. The compiler will check types implicitly via type inference and give compile-time errors, but it also enables programs to bypass type-checking and rely on a target platform's dynamic type-handling. All of the native target APIs can be used.
Type system
[edit]Haxe has a sophisticated and flexible type system. The type kinds it offers are classes, interfaces, function-method types, anonymous types, algebraic data types (called enum in Haxe), and abstract types. Parametric polymorphism is possible with classes, algebraic types and function types, giving the language support for generic programming based on type erasure. This includes support for variance in polymorphic functions, although not in type constructors.
The type system is static unless annotations for dynamic typing are present, for use with targets that support them. Type checking follows nominal typing with the exception of anonymous types where structural typing is used instead. Finally, type inference is supported, allowing for variable declarations without type annotations.
Modules and namespaces
[edit]All Haxe code is organized in modules, which are addressed using paths. In essence, each .hx file represents a module which may contain several types. For example, to create the type A
in the package my.pack as shown, the folder structure should be my\pack and the file could be A.hx in the folder pack.
// file my/pack/A.hx
package my.pack;
class A {}
In other modules, other types can be imported by putting import
statements below the package definition, e.g. import my.pack.A;
A module can contain multiple types, such as the following. It is possible to import one type at a time from that module, using import my.pack2.A;
. A type may be private
, in which case only its containing module can access it.
package my.pack2;
typedef A = {a:String}
private typedef B = {b:String}
Classes
[edit]Classes (keyword class
) in Haxe are similar to those in Java or TypeScript. Their fields can be either methods, variables, or properties, each static or per instance respectively. Haxe supports the accessors public
and private
, and more advanced methods for access control that are denoted using annotations. Methods and static constant variables can be inlined using the keyword inline
. Fields can be marked as final
to declare a constant that must be initialized immediately or in the constructor and cannot be written to, in case of function final
will mark as non-overridable in subclasses.
Interfaces in Haxe are very similar to those in, for example, Java.
interface ICreature {
public var birth:Date;
public var name:String;
public function age():Int;
}
class Fly implements ICreature {
public var birth:Date;
public var name:String;
public function age():Int return Date.now().getFullYear() - birth.getFullYear();
}
Generics
[edit]Haxe supports generic programming. The following is an example of the identity function.
function identity<T>(arg:T):T {
return arg;
}
Enumerated types
[edit]Enumerated types are an important feature of the language; they can have type parameters and be recursive.[20] They provide basic support for algebraic data types, allowing the inclusion of product types, in a fashion similar to Haskell and ML. A switch
expression can apply pattern matching to an enum value, allowing for elegant solutions to complex programming problems:
enum Color {
red;
green;
blue;
rgb(r:Int, g:Int, b:Int);
}
class Colors {
static function toInt(c:Color):Int {
return switch (c) {
case red: 0xFF0000;
case green: 0x00FF00;
case blue: 0x0000FF;
case rgb(r, g, b): (r << 16) | (g << 8) | b;
}
}
static function validCalls() {
var redint = toInt(Color.red);
var rgbint = toInt(Color.rgb(100, 100, 100));
}
}
Examples of parametric enum types are the Haxe standard library types Option[21] and Either:[22]
enum Option<T> {
Some(v:T);
None;
}
enum Either<L, R> {
Left(v:L);
Right(v:R);
}
Haxe also supports generalized algebraic data types (GADTs).[23][24]
Anonymous types
[edit]Anonymous types are defined by denoting their structure explicitly, using a syntax that follows the mathematical record-based representation of a type. They can be used to implement structural typing for function arguments (see below), and can be given an alias with the keyword typedef
:
typedef AliasForAnon = { a:Int, b:String, c:Float->Void };
Function types
[edit]Functions are first-class values in Haxe. Their type is denoted by using arrows between argument types, and between the argument type(s) and return type, as common in many functional languages. However, unlike in prominent examples like Haskell or the ML language family, not all functions are unary functions (functions with one argument only), and in Haxe, functions can't be partially applied per default. Thus, the following type signatures have different semantics than in the aforementioned languages. The type F1
is a function that takes a String
as arguments and returns a value of type Float
.
Types F1
and F2
denote the same type, except that F2
uses labelled parameter, which is useful for completion and documentation.
Types F4
and F5
denote the same type. Both are binary functions that return a binary function of type F3
. For F5
the syntax to declare a function type within a function type is used.
typedef F1 = String -> Float;
typedef F2 = (text:String) -> Float;
typedef F3 = (score:Int, text:String) -> Float;
typedef F4 = (score:Int, text:String) -> F3;
typedef F5 = (score:Int, text:String) -> ((score:Int, text:String) -> Float);
Anonymous functions
[edit]In Haxe, anonymous functions are called lambda, and use the syntax function(argument-list) expression;
.
var f = function(x) return x*x;
f(8); // 64
(function(x,y) return x+y)(5,6); // 11
Abstract types
[edit]A relatively new addition to the Haxe type system is a concept termed abstract types. As used in Haxe, this refers to something different from a conventional abstract type. They are used to make conversions between types implicit, allowing reuse of existing types for specific purposes, like implementing types for units of measurement. This greatly reduces the risk of mixing up values of the same underlying type, but with different meanings (e.g., miles vs. km).
The following example assumes that the metric system is the default, while a conversion to miles is needed for legacy data. Haxe can automatically convert miles to kilometers, but not the reverse.
abstract Kilometer(Float) {
public function new(v:Float) this = v;
}
abstract Mile(Float) {
public function new(v:Float) this = v;
@:to public inline function toKilometer():Kilometer return (new Kilometer (this / 0.62137));
}
class Test {
static var km:Kilometer;
static function main(){
var one100Miles = new Mile(100);
km = one100Miles;
trace(km); // 160.935
}
}
As the example shows, no explicit conversion is needed for the assignment "km = one100Miles;" to do the right thing.
Abstract types are entirely a compile-time feature of Haxe and do not exist at all at program runtime. As an example, both variables using abstract types above Mile
and Kilometer
will be of the type Float
at runtime.[25]
Structural typing
[edit]In many functional programming languages, structural typing plays a major role. Haxe employs it in the presence of anonymous types, using the nominative typing of object-oriented programming, when only named types are involved. Anonymous types in Haxe are analogous to the implicit interfaces of the language Go as to typing. In contrast with Go interfaces, it is possible to construct a value using an anonymous type.
class FooBar {
public var foo:Int;
public var bar:String;
public function new() {
foo = 1;
bar = "2";
}
function anyFooBar(v:{foo:Int, bar:String})
trace(v.foo);
static function test() {
var fb = new FooBar();
fb.anyFooBar(fb);
fb.anyFooBar({foo: 123, bar: "456"});
}
}
Internal architecture
[edit]Compiler
[edit]The Haxe compiler is divided into one frontend and multiple backends. The frontend creates an abstract syntax tree (AST) from the source code, and performs type checking, macro expansion, and optimization on the AST. The various backends translate the processed AST into source code or generate bytecode, depending on their target.
The compiler is written in OCaml. It can be run in server-mode to provide code completion for integrated development environments (IDEs) and maintain a cache, to further speed compiling.[26]
See also
[edit]- Dart (programming language)
- Nim (programming language)
- Opa (programming language)
- Clojure
- CoffeeScript
- TypeScript
- Scala (programming language)
- Vala (programming language)
- Emscripten
- OpenFL
References
[edit]- ^ "Release 4.3.6". 7 August 2024. Retrieved 22 August 2024.
- ^ "Open Source licence Haxe".
- ^ Introduction to the Haxe Standard Library, Haxe Docs
- ^ Target Specific APIs, Introduction to the Haxe Standard Library, Haxe Docs
- ^ Doucet, Lars (2014-06-24). "Dear Adobe: Support Haxe, save your Tools". Gamasutra.
- ^ "Haxe Interview". Io Programmo. 2009-04-01. pp. 1–6. Archived from the original on 2015-12-08. Retrieved 2015-08-06.
- ^ "Hello Lua!". Haxe.org.
- ^ "hxnodejs".
- ^ a b "Compiler Targets". Haxe. Retrieved 2021-11-05.
- ^ List of IDEs supporting Haxe, Haxe Foundation
- ^ "Release alpha-1". haxe. HaxeFoundation. 2005-11-14. Retrieved 2022-04-02 – via GitHub.
- ^ Cannasse, Nicolas (2005-11-17). "Haxe Alpha 1b". haXe (Mailing list). Archived from the original on 2007-10-26.
- ^ "Nicolas' announcement of spelling change on Haxe official mail list".
- ^ "Haxe mailing list post on naming". Archived from the original on 2007-03-28.
- ^ MTASC Compiler, MTASC website
- ^ "Eval - The new Haxe macro interpreter".
- ^ "Compiler Features".
- ^ "Macros in Haxe".
- ^ Dyachenko, Vadim (2013-12-05). "On "You can't make good HTML5 games in Haxe"". Yellow After Life.
- ^ "Haxe reference detailing the use of enum". Archived from the original on 2012-05-11.
- ^ "haxe/Option.hx at development · HaxeFoundation/haxe". Github. 7 November 2021.
- ^ "haxe/Either.hx at development · HaxeFoundation/haxe". Github. 7 November 2021.
- ^ "Language Features". Haxe - The Cross-platform Toolkit. Haxe Foundation. Retrieved 30 April 2015.
- ^ "haxe/TestGADT.hx at development · HaxeFoundation/haxe". Github. 7 November 2021.
- ^ "Abstract". Haxe - The Cross-platform Toolkit. Retrieved 2023-04-24.
- ^ Server mode command-line:
haxe --wait [host:]port
External links
[edit]- High-level programming languages
- Functional languages
- Multi-paradigm programming languages
- Object-based programming languages
- OCaml software
- Pattern matching programming languages
- Programming languages
- Programming languages created in 2005
- Software using the GPL license
- Source-to-source compilers
- Statically typed programming languages