HaxeFoundation / HaxeFoundation/haxe

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

Open
#10,971 1 comment 0 reactions 0 assignees View on GitHub
platform-hl
Dominant language
Haxe
Stars
6.9k
Forks
715
Avg merge
2d 2h
Merged PRs (30d)
11

Description

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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.