HaxeFoundation / HaxeFoundation/haxe

Compile error due to wrong implicit conversion being chosen

Open
#7,243 0 comments 0 reactions 1 assignee Claimed by @Simn View on GitHub
feature-abstracts
Dominant language
Haxe
Stars
6.9k
Forks
715
Avg merge
2d 2h
Merged PRs (30d)
11

Description

Here's a rather minimal example (based on some experiments I was doing with `tink_core`):
```haxe
class Test {
static function main() {
var f:Future = 10;
trace(f.gather().get());
}
}

@:forward(get, gather)
abstract Future(FutureObject) from FutureObject to FutureObject {
@:from static inline function ofAny(v:T):Future
return new SimpleFuture(v);
}

private interface FutureObject {
function get():T;
function gather():Future;
}

private class SimpleFuture implements FutureObject {
var value:T;

public function new(value:T)
this.value = value;

public function get():T return value;

public function gather():Future
return gatherFuture(this); // Compile error, the `ofAny` implicit cast is being chosen
//return gatherFuture((this:FutureObject)); // Works
//return gatherFuture((this:Future)); // Works
//return gatherFuture(cast this); // Works

static public function gatherFuture(f:Future):Future
return f;
}
```
https://try.haxe.org/#e7C09

```
Test.hx:30: characters 8-33 : Future<_Test.SimpleFuture<_Test.SimpleFuture.T>> should be Future<_Test.SimpleFuture.T>
Test.hx:30: characters 8-33 : Type parameters are invariant
Test.hx:30: characters 8-33 : _Test.SimpleFuture<_Test.SimpleFuture.T> should be _Test.SimpleFuture.T
```

It fails to compile because for some reason the compiler chooses the abstract's `@:from ... ofAny` implicit cast rather than the abstract's concrete type `from` cast. Example includes various workarounds which all work.

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.