authors are vetted experts in their fields and write on topics in which they have demonstrated experience. All of our content is peer reviewed and validated by Toptal experts in the same field.
Kevin Bloch
Verified Expert in Engineering
25 Years of Experience

Kevin has more than 25 years among full-stack, desktop, and indie game development. 他最近专门研究PostgreSQL、JavaScript、Perl和Haxe.

Share

Our previous Haxe review 最后我们来看看即将到来的Haxe 4. With the official release of Haxe 4 (and shortly thereafter, two bug-patch releases—version 4.0.1 and 4.0.2), it’s time for a new Haxe review. What are the latest additions to this burgeoning programming language? Haxe编程语言社区的发展方向是什么? Are Haxe game engines still its mainstay?

Haxe Review: Haxe 4’s New Features

With more than three years of development since the last major release, version 4 of the Haxe programming language improves macro performance, developer experience, and syntax. Three of its enhancements are still considered experimental but worth highlighting: the new JVM bytecode target, support for inline markup, and null safety checks.

hax4中实验性JVM字节码编译目标

Haxe 4’s new JVM bytecode target makes Java development via Haxe quite a bit more efficient by cutting out a major compilation step: There’s no second step of having Java’s own compiler (javac)编译Haxe的转译器的Java源代码输出.

A comparison of the new direct JVM target with the original workflow when developing for a Java target. The original takes some .hx source, produces .java source, which in turn needs to be compiled with a Java compiler (which depends on the JDK) before a runnable .jar file is finally produced. 新的目标允许开发者直接从 .hx source to a runnable .jar file.

This method of compiling with Haxe 4 also entirely removes dependency on the Java developer kit (JDK), and opens the door for interactive debugging to be implemented in the future.

Until hxjava的主流版本是Haxe 4兼容的, 基本设置包括安装Haxe和Haxelib, then running haxelib install hxjava 4.0.0-alpha. 完成这些后,开发流程就很简单了:

# transpile directly to JVM bytecode with Haxe (-D jvm would also work):
java jav_output——定义jvm

# run JVM bytecode with Java:
java -jar jar_output/HelloWorld.jar

Given that direct JVM compilation still has experimental status in Haxe 4, it comes with a couple of caveats:

Nonetheless, it’s a notable step in the right direction for anyone leveraging Java-based technologies.

hax4中的实验性内联标记支持

JSX, anyone? Haxe 4 enables inline markup, allowing developers to write, for example, HTML directly within Haxe source code:

    var dom = jsx(
      

Hello!

This is a paragraph.

);

Since jsx() here can be a static macro function, this allows a project to have compile-time checks as to whether the markup conforms to whatever XML-ish spec the developer cares to implement. Since XML support itself is built into the Haxe API, the check can leverage Xml.parse(), but 对于基本的“类似xml”的可解析性,甚至不需要:

  static macro function jsx(expr) {
    return switch expr.expr {
      case EMeta({name: ":markup"}, {expr: EConst(CString(s))}):
      	macro $v{"XML MARKUP: " + s};
      case _:
        throw new haxe.macro.Expr.Error("not an xml literal", expr.pos);
    }
  }

The intention with this feature is to help push Haxe out of the game development bubble (although it surely has uses there too). It’s general enough that it’s implemented at the compiler level—hence not needing the Haxe API in the above macro—but checking for specific DSLs is the next question for the compiler team and the community to figure out.

Experimental Null Safety in Haxe 4

自1965年零引用发明以来, the issue of null safety has often been the bane of developers in nullable typed environments like that of the Haxe programming language. Aleksandr Kuzmenko estimates that GitHub commits fixing null pointer reference errors number over 10 million.

Haxe 4 has built-in compile-time null safety macros, which can be enabled by including a @:nullSafety line just before a given definition. It comes in @:nullSafety(Loose) (the default) and @:nullSafety(Strict) modes, and can be disabled as needed with @:nullSafety(Off). Strict mode will look through function calls for field mutations that might assign null, even in a null safety–disabled context.

Ruby开发人员可能想知道方便的安全导航操作符(?. in Ruby) is on the radar. 还没有,但是就像Haxe编程的许多方面一样,有 a macro for that (note that it uses !. instead.)

Developer Experience (DX) with Haxe 4: Syntax Additions, Syntactic Sugar, and More

The DX-related additions to the Haxe programming language and Haxe IDE support bring the Haxe 4 experience at least level with other programming languages on various fronts. In some ways, Haxe seeks to be everything to everyone, but the compiler team takes a thoughtful approach toward integrating the most useful features and conventions from other languages.

The result is that the Haxe programming language and standard API evolve without compromising their stability, sensibility, and cohesiveness. 并非Haxe评论中的所有内容都值得大肆宣传, 而这正是关键所在:DX正在进步, 这有利于简单地追求华丽的“功能” du jour.”

There’s a balance to be had, though: Haxe’s changes are done with an awareness of the patterns other languages are following, and Haxe 4 certainly makes an effort to appeal to newcomers from more popular languages.

New “Function Type” Syntax

On that note, Haxe now supports two major ways of representing function types. The old syntax “suggests that auto-currying and partial application are supported, but they aren’t,” according to the original feature proposal:

Int -> String -> Void

新语法允许命名参数,这改进了DX:

(id:Int, name:String) -> Void

But DX aside, using Haxe 4’s new syntax for function types is a good habit to get into, as the old, 低级语法可能会在将来的主要版本中删除.

Syntactic Sugar…Sort of

Perhaps it’s not groundbreaking, but Haxe 4’s syntactic improvements will be welcome news both for existing Haxe developers with certain development backgrounds (ES6, for example) and those who might be coming from them to Haxe for the first time.

现在支持箭头函数(" short lambda ")语法, 在Haxe的例子中,这或多或少只是一种打字的快捷方式 function and return. 键值和索引值迭代语法(用于映射和数组), respectively) are now supported too. 使用静态扩展的类型声明只能使用一个 using statement globally instead of needing them everywhere the corresponding static extension methods are used.

枚举和枚举抽象还有其他一些改进, one of which is that the latter have moved from the purview of macros to having direct compiler support. Other features moved similarly include final classes, final interfaces, and extern fields.

Some features relying on macros stayed reliant on macros, but improved nonetheless. 运算符重载被升级为包括字段设置器, and metadata can now be namespaced with . separators as in @:prefix.subprefix.name.

Calling the above changes syntactic sugar is admittedly oversimplifying, but readers are welcome to dig into the original proposals linked to from Haxe 4’s release notes where they need more detail.

More Haxe 4 DX Boosts

While interactive debugging was 已经可以在Haxe中用于各种编译目标, the new eval 目标使得交互式调试解释代码成为可能. For a simple example, you can take any Haxe “Hello, World” tutorial’s project directory, add a file called whatever-you-want.hxml looking like this:

--main HelloWorld
--interp

并在VSCode IDE中进行交互式调试,只需:

  1. Opening the project directory in VSCode;
  2. Adding a breakpoint somewhere; and
  3. 按F5并从下拉菜单中选择“Haxe Interpreter”.

This feature also allows you to interactively debug macro code the same way, 即使你是在编译一个特定的目标,比如 java (rather than using --interp). The only installation requirement besides Haxe and VSCode themselves is the Haxe VSCode extension.

IDE Services

Speaking of IDEs, Haxe 4引入了一个新的IDE服务协议, 这已经在最新的VSCode Haxe扩展杠杆, vshaxe. Besides a significant performance boost, 这允许vshaxe提供一些非常有用的DX改进, including:

  • (Long awaited) automatic imports
  • 自动完成悬停提示显示更多细节, like answering the question, “Where is this field from?”
  • 非常彻底的自动完成在几个光滑的新方式, like expected type completion, postfix completion, and override completion
  • 在输入代码时进行按键优化

看这些的值要容易得多 来自相关vshaxe变更日志的优秀可视化演示. vshaxe with VSCode is not the only Haxe IDE around—HaxeDevelop and Kode Studio are Haxe-specific, IntelliJ IDEA有Haxe IDE插件, Sublime Text, Atom, etc.—but it does seem to be ahead of the pack in terms of making use of Haxe 4’s new IDE services protocol, followed closely by IntelliJ-Haxe.

Unicode Literals

Developers wanting to use real Unicode string literals will find support for that in Haxe 4, but there are some nuances to be aware of.

Read-only Arrays

标准Haxe API现在具有只读数组. These are as easy to use as declaring a variable to be of the type, for example, haxe.ds.ReadOnlyArray, after which trying to set, push, or pop values results in various compiler errors. Add the final keyword to the declaration, and reassigning the array itself will be disallowed as well.

Call-site Inlining

Call-site inlining is a new Haxe language feature allowing developers fine-grained control over where functions are inlined, useful when optimizing frequently called functions where the overall size-performance tradeoff might otherwise be a lose-lose decision.


These are worthwhile additions to the already excellent Haxe programming language. What are developers in the Haxe community building now that Haxe 4 is out?

超越Haxe游戏引擎:Web开发与Haxe 4

Haxe的用户主要是游戏程序员. But there are plenty of examples Haxe在其他领域的大规模应用, like business stacks, mobile apps, and the web, both for front- and back-end development.

To that end, Haxe 4 supplies regenerated HTML externs, meaning that Haxe’s js.html standard API has been brought up to date with the wider web API as MDN defines it, 以及修复bug和添加缺失的api. (For example, Haxe 4 now includes the Push API.)

In Juraj Kirchheim’s talk, Weaving a Better Web with Haxe, he points to examples of Haxe-based web solutions being orders of magnitude more efficient—yet also more robust—in an enterprise setting.

He also argues against the Rails architectural approach (in terms of folder hierarchy), but developers who favor a complete web framework a la Rails can still find one. Other times, it may benefit developers to review the source of a complete web project, 在这种情况下,值得一看 public repo for Giffon这是一个支持Haxe 4的众包平台. Likewise, web-centric, open-source Haxe libraries like the JavaScript-splitting Haxe Modular, the generic thx.core and its sister libraries, and the venerable Haxe web toolkit Tinkerbell all already support Haxe 4. So does the cross-platform UI solution HaxeUI, which supports a web context but targets a much wider scope including business and desktop app development; it in particular has steadily continued to mature leading up to the new Haxe language release.

Web, games, enterprise…regardless of the platform and app flavor a development team—even a team of one—is targeting, Haxe developers will eventually have to grapple with managing dependencies. For this, a helpful resource for Haxe developers to review is the slides from Adam Breece’s talk, Scaling well with others.

Haxe是游戏的最佳编程语言

是否存在一种“最佳”游戏开发语言? It’s a subjective question, and an easy one to find heated debates about. 比人们从它的社区规模上想象的要大, Haxe’s success in the game development sphere is certainly not coincidental. 乔·威廉姆森(Joe Williamson)提供了一些见解,解释了为什么会出现这种情况 关于赢得2019年Ludum Dare 45游戏jam的采访这种情况似乎可能会在Haxe 4中继续下去.

Haxe’s original creator, Nicolas Cannasse, is already using Haxe 4 in production with Shiro Games’ Northgard. Motion Twin也在生产中使用Haxe 4 Dead Cells. 这两款游戏在Steam上都有成千上万的好评, and are available for both PCs (Win, Mac, and Linux) and consoles—a truly formidable result considering that both games have smaller development teams yet user bases in the millions. Dead Cells even has an iOS version, with an Android version on their radar as well.

Library-wise, several major Haxe game engines are definitely keeping step with Haxe 4’s changes. Haxe 4–compatible engines include Kha (以及建立在它之上的许多引擎的一部分).g., Armory), HaxeFlixel and its main dependency OpenFL, NME, and Heaps——当然,因为《欧博体育app下载》和《欧博体育app下载》就是这么用的. HaxePunk is also working on Haxe 4 compatibility; in one case, a library, Nape, was forked to work with Haxe 4.

Some developers also make their own engines instead of using one of the many already out there. For example, Kirill Poletaev, who details how and why he wrote his own 3D Haxe game engine. Since said engine is in-house, it makes sense that it’s one example of a project that has not migrated to Haxe 4 yet.

第4点:继续一个优秀的工具链的顺利进展

With Haxe having such wide utility, Haxe 4最重要的特性因开发人员而异, 所以Haxe的评论并不详尽. Haxe 4的一些变化在上面遗漏了,包括:

  • 为JavaScript目标添加ES6输出
  • 删除的功能(其中一些仍然可以通过 hx3compat library) and targets (PHP5 and soon AS3)
  • CLI标志与常用工具(-lib-using .hxml files will need changing to -L or --library, for example).
  • Besides final now being a keyword (which therefore can’t be used as a variable name), operator and overload are also newly reserved keywords.

There were also some breaking changes, but they are so few that many actively maintained libraries aren’t even bothering to explicitly announce Haxe 4 compatibility—in general, 从Haxe 3迁移据说是相当简单的. After all, one of Haxe’s goals is stability in the midst of juggling support for a high number of target platforms, and Haxe 4 does not disappoint here.

What about new users? In the end, it’s up to the reader to decide whether Haxe is the best coding language for games, whether the Haxe ecosystem offers the most robust libraries for web development, or whether Haxe-specific tooling gives the most sensible DX for a particular workflow. At the very least, Haxe在许多领域仍然是一个可行的竞争者, offering something of a secret advantage for just about any developer.

Further reading: Developers new to Haxe may be interested in a Haxe tutorial by John Gabriele, and also the release notes of Haxe 4.1.0 and later versions.

Understanding the basics

  • 哪个框架可以用于开发跨平台应用程序?

    There are several cross-platform frameworks like React Native and Xamarin, but Haxe can be used to develop applications for multiple platforms from a single codebase with or without a framework. React Native特别提供了Haxe extern, 这意味着可以使用Haxe进行开发.

  • Which programming language is the best for cross-platform development?

    While Java is often the first language discussed in a cross-platform context, Haxe has an even wider set of targets: It’s able to transpile to Java, but also C#, C++, JavaScript, PHP, and several others. Since Haxe has managed to keep so many targets while polishing its usability, it’s a definite contender here.

  • 哪种编程语言最适合做游戏?

    Game development spans a huge spectrum, so the best programming language for games depends on both the programmer and the project. The case for Haxe is that it offers a uniquely refined language while being able to simultaneously target Win/Mac/Linux, iOS/Android, HTML5, and console systems from a single codebase.

  • What is a game development engine?

    Game development engines are software libraries that provide common (or sometimes complete) game engine functionality. A game engine consists of the core code needed to process assets, user plugins, etc. to provide a game experience. A game development engine may include its own IDE and other specific tools.

Hire a Toptal expert on this topic.
Hire Now
Kevin Bloch

Kevin Bloch

Verified Expert in Engineering
25 Years of Experience

Bergerac, France

Member since January 31, 2017

About the author

Kevin has more than 25 years among full-stack, desktop, and indie game development. 他最近专门研究PostgreSQL、JavaScript、Perl和Haxe.

authors are vetted experts in their fields and write on topics in which they have demonstrated experience. All of our content is peer reviewed and validated by Toptal experts in the same field.

World-class articles, delivered weekly.

输入您的电子邮件,即表示您同意我们的 privacy policy.

World-class articles, delivered weekly.

输入您的电子邮件,即表示您同意我们的 privacy policy.

Toptal Developers

Join the Toptal® community.