haxe icon indicating copy to clipboard operation
haxe copied to clipboard

[bug/qol?] abstract over map instantiated with `[]` gets typed to an array

Open Jarrio opened this issue 3 months ago • 1 comments

class Test {
	static function main() {
		var map:Foo = [];
	}
}

abstract Foo(Map<String, Int>) from Map<String, Int> {
	public function new(value:Map<String, Int> = null) {
		this = value ?? [];
	}
}

https://try.haxe.org/#4844a077 This fails to compile however the below works fine

var map:Map<String, Int> = []

Jarrio avatar Sep 12 '25 21:09 Jarrio

Workaround for empty array literal: https://try.haxe.org/#229CCBDE

  @:from static macro function fromEmptyArray(e) {
    return switch haxe.macro.Context.typeExpr(e).expr {
      case TArrayDecl([]): macro new Foo();
      case _: e;
    }
  }

Note that it won't handle this:

var a = [];
var map:Foo = a;

kLabz avatar Sep 15 '25 09:09 kLabz