haxe icon indicating copy to clipboard operation
haxe copied to clipboard

`@:using` not working on types inferred from inline & cast-only functions

Open fal-works opened this issue 3 years ago • 1 comments

Haxe v4.2.5 - nightly 651ecf3

Sometimes I'd like to have inline functions just for type cast (like castMyType() in the code below),
rather than typing explicitly (like var value:MyType = {num: 0} or ({num: 0} : MyType) ).

However it seems that @:using does not work with such kind of code under some conditions.
And it depends on the target which functions are considered "cast only".

Code expected to compile but actually not:

// ---- Main.hx ----

function main() {
	final value = castMyType({num: 0}); // vshaxe says value is MyType. No error if explicit type hint
	value.doSomething(); // Compiler error on static targets: "{ num : Int } has no field doSomething"
}

// No error if inline removed
inline function castMyType(x:MyType):MyType {
	// No error if some other code here
	return x;
}

@:using(Main.MyTypeTools) // No error if traditional `using` instead
typedef MyType = {
	final num:Int; // Changing final to var raises the same error even on dynamic targets
}

class MyTypeTools {
	public static function doSomething(x:MyType)
		trace("hello");
}

(I doubt this is a duplicate of another issue, though I didn't find a similar one)

fal-works avatar Apr 28 '22 12:04 fal-works

A workaround would be to do return (x:MyType); in your inline function, which seems a little too much inlined (losing type hint?) in static targets currently for some reason:

final value = castMyType({num: 0});
$type(value); // MyType on dynamic targets, {num: Int} on static targets

kLabz avatar Apr 28 '22 12:04 kLabz