Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type.getInstanceFields returns extra empty string in HL when inheriting interface #10971

Open
nspitko opened this issue Feb 17, 2023 · 1 comment
Labels
platform-hl Everything related to HashLink
Milestone

Comments

@nspitko
Copy link

nspitko commented Feb 17, 2023

A try.haxe link: https://try.haxe.org/#38AB9237

And the code for posterity:

interface CDObject {

}

@:keepSub
@:structInit class CUIObject implements CDObject {

	public var name: String = null;

	public function clone( )
	{
		var cls = Type.getClass(this);
		var inst = Type.createEmptyInstance(cls);
		var fields = Type.getInstanceFields(cls);
		for (field in fields)
		{
			// generic copy
			var val:Dynamic = Reflect.field(this,field);
			if ( !Reflect.isFunction(val) )
			{
				Reflect.setField(inst,field,val);
			}
		}
		return inst;
	}
}

class Test {
  static function main() {
    var foo: CUIObject = {};
    foo.clone();
    trace("OK!");
  }
}

This code fails, as the last entry in fields is an empty string (and trying to touch empty string as a field results in an expected error)

This only happens in HL, and only when the object has an interface. If we remove the implements then the code works correctly.

@kLabz
Copy link
Contributor

kLabz commented Feb 17, 2023

Removed some noise to extract as test:

interface Bar {}
class Foo implements Bar {}

class Test {
  static function main() {
    var fields = Type.getInstanceFields(Foo); // should be []
    
    // Print extra field(s)
    for (field in fields) trace('field "$field"'); // field ""
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
platform-hl Everything related to HashLink
Projects
None yet
Development

No branches or pull requests

3 participants