-
-
Notifications
You must be signed in to change notification settings - Fork 662
What's new in Haxe 4
We’re introducing the new syntax for specifying function types, now with clear separation of arguments from the return type and with support for argument names, which is very helpful for code self-documenting and better IDE support.
Example:
(name:String, age:Int)->Bool
[Evolution proposal](https://github.com/HaxeFoundation/haxe-evolution/pull/23)
Note that the old syntax is still supported, but the new one is preferable.
Haxe now finally has the long-awaited arrow function syntax (a.k.a. short lambdas)! This is useful for code that has a lot of "short-and-sweet" callback functions, especially for the code written in the functional paradigm or the code that has to deal with asynchronous operations.
Example:
(a, b) -> a + b
[Evolution proposal](https://github.com/HaxeFoundation/haxe-evolution/pull/8)
@:nullSafety
final a = 5
final class Foo {}
final interface Bar {}
"人".length == 1
faster interpreter → faster macros and scripts debugging support \o/
bytecode and C, run-time fully conforming to haxe "spec"
It’s now possible to inline function calls and inlineable class instantiations by specifying inline
keyword before call/new expression:
inline f()
inline new Obj()
[Evolution proposal](https://github.com/HaxeFoundation/haxe-evolution/pull/45)
It’s now possible to enable [static extensions (a.k.a. using
)](https://haxe.org/manual/lf-static-extension.html) for a type at that type’s declaration place. This is particularly useful for adding additional methods for enum
types, for example:
@:using(Outcome.OutcomeTools)
enum Outcome<T> {
Success(value:T);
Failure(error:String);
}
class OutcomeTools {
public static function sure<T>(outcome:Outcome<T>):T {
switch outcome {
case Success(value): return value;
case Failure(error): throw error;
}
}
}
The static extension methods from OutcomeTools
can then be used without adding explicit using Outcome.OutcomeTools;
statement:
class Main {
static function main() {
var outcome = load();
outcome.sure();
}
static function load():Outcome<String> {
return Success("Done!");
}
}
Abstract types now support the "set" version of @:op(a.b)
operator functions (aka "resolve")
abstract DotAccess<T>(Map<String,T>) {
public function new() {
this = new Map();
}
@:op(a.b) function get(field:String):T {
return this[field];
}
@:op(a.b) function set(field:String, value:T):T {
return this[field] = value;
}
}
...
var d = new DotAccess();
d.hello = 5;
trace(d.hello);
The for
loop syntax now supports iteration over key+value pairs with a new syntax:
for (key => value in collection) {}
User-defined structures can support this by conforming to the new [KeyValueIterable](https://api.haxe.org/v/development/KeyValueIterable.html) protocol.
[Evolution proposal](https://github.com/HaxeFoundation/haxe-evolution/blob/master/proposals/0006-key-value-iter.md)
var a = <hi/>;
```
This will be available for macro processing as `@:markup "<hi/>"`.
XML strings are supposed to be parsed with a custom parser.
Simple example to convert inline XML to a string with a macro function:
```haxe
class Test {
static function main() {
var dom = jsx(<hi/>);
trace(dom);
}
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);
}
}
}
A more complete example of parsing JSX-like syntax in @ncannasse’s DOMKit: https://github.com/ncannasse/domkit/blob/master/domkit/MarkupParser.hx
{ var ?f:Int; }
enum abstract E(T) {}
enum abstract E(Int) {
var A; // 0
var B; // 1
}
enum abstract E(String) {
var A; // "A"
var B; // "B"
}
extern inline function f(...) {}
Use custom abstracts if you need something like that
A & B
(currently only for structures and type param constraints)
C<T:(A,B)>
now should be C<T:A&B>
var map:Map<Int, String> = [];
var a:haxe.ds.ReadOnlyArray<Int> = [1, 2, 3];
a.push(4); // haxe.ds.ReadOnlyArray<Int> has no field push
more features, json-based, currently undocumented, implemented in haxe language server and used in vshaxe
Learn Haxe: Introduction | Manual | API | Try Haxe | Haxe Library Manager | Code Cookbook
Connect: GitHub | Stack Overflow | Forum | Discord | Gitter | Twitter | Facebook