What's new in Haxe 4.2 - HaxeFoundation/haxe GitHub Wiki

This release brings a number of more conventional language features to Haxe.

Module-level fields

It is now possible to define variables and methods directly in modules, without creating a class:

function main() {
	trace("Hello from a module method!");
}

Read more about this feature in the Haxe Blog.

Abstract classes

Classes and their methods can now have the abstract modifier. For classes it means that it's not possible to create instances of this class, even if it has a constructor. For methods it means that such methods cannot have an implementation and must be provided in child classes, similarly to interface methods. Any non-abstract class extending an abstract one must provide implementations of all its abstract functions.

abstract class Base {
	abstract function getValue():Int;
}

class Sub extends Base {
	function getValue():Int {
		return 42;
	}
}

Read more about this in the Haxe Evolution Proposal.

Method overloading

We introduced proper support for method overloading for extern methods. It is now possible to define multiple variants of an extern method with the same name, but different signatures by using the overload modifier.

extern class C {
	function new();
	overload function f(i:Int):Void;
	overload function f(a:String):Void;
}

function main() {
	var c = new C();
	c.f(42);
	c.f("hello");
}

Note that it is also possible to use overloading with extern inline methods with non-extern classes or module-level fields:

overload extern inline function f(i:Int) {
	trace("int: " + i);
}

overload extern inline function f(s:String) {
	trace("string: " + s);
}

function main() {
	f(42);
	f("hello");
}

Support for non-extern method overloading might be added in future.

Rest arguments

Haxe now has unified rest/variadic arguments support for both normal and extern functions, including new syntax for declaring and applying rest arguments using the new unary "spread" operator:

function printf(format:String, ...rest:Any):String {
	return "implement me";
}

function main() {
	var args = [1, 2];
	printf("%d %d", ...args);
}

Per-thread event loop

On targets that have threading support it is now possible to attach an event loop to a thread. The EventLoop API allows adding "event" functions to be executed in a thread, automatically keeping the thread alive until there are no more events scheduled.

import sys.thread.Thread;

function main() {
	var currentThread = Thread.current();
	currentThread.events.run(() -> trace("event!"));
}

This is an important step for implementing cross-platform asynchronous I/O API we have planned.

Note that haxe.Timer will now always execute its callbacks in the thread in which it was created.