HaxeFoundation / HaxeFoundation/hashlink

Invalid downcast from interface

Open
#339 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C
Stars
901
Forks
201
Avg merge
18h 9m
Merged PRs (30d)
8

Description

The following produces invalid cast:

```haxe
interface I {
function foo() : Int;
}

class Main implements I {

var x : Int = 66;

public function new() {
}

public function foo() {
return x;
}

static function test( x : Dynamic ) {
var p = Std.downcast(x,Main);
return p.x;
}

static function main() {
var x : I = new Main();
trace( test(x) );
}

}
```

This is because Std.downcast is implemented using `if( getType(x).check(Main) ) unsafe_cast(x) else null` and the unsafe cast does not get back the original value from the interface, meaning the returned value is the interface and not the class instance.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.