HaxeFoundation / HaxeFoundation/haxe

Consider `toString` static extension in string coercion

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

Description

AFAF: https://try.haxe.org/#52ff1319

```haxe
@:using(Test.MyEnumUtils)
enum A {
VA;
VB(isX:Bool);
}

abstract B(A) from A {
@:to function toString() return this.toString();
}

class MyEnumUtils {
public static function toString(v:A) return switch v {
case VA: "this is va";
case VB(false): "this is vb false";
case VB(true): "this is vb true";
};
}

class Test {
static function main() {
var m = VB(true);
trace(m);// toString is not called
trace('$m');// toString is not called
trace(m.toString());// toString is called ... obviously
var m:B = m;
trace(m);// toString is called
trace('$m');// toString is called
}
}
```

In the `trace('$m')` case the compiler explicitly inserts a `Std.string` and to expect the `toString` to be used there if available seems quite reasonable. In the `trace(m)` case I'm not so sure what the behavior should be, but FWIW I think the best option would be to make it work the same as it works for abstracts.

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.