HaxeFoundation / HaxeFoundation/haxe

abstract and String concat

Open
#8,627 15 comments 1 reaction 0 assignees View on GitHub
feature-abstracts
Dominant language
Haxe
Stars
6.9k
Forks
715
Avg merge
2d 2h
Merged PRs (30d)
11

Description

```haxe
abstract Decimal(Float) from Float to Float {
@:from static function fromString(value : String) : Decimal
return Std.parseFloat(value);

@:to public function toString() : String
return Std.string(this);

@:op(A+B) @:commutative
function add(that : Decimal) : Decimal
return this + (that:Float);
}

class Test {
static function main() {
var d:Decimal = 123.4;
trace("str: " + d); // Test.hx:16: NaN, but I expect it to be "str: 123.4"
trace("str: " + d.toString()); // Test.hx:17: str: 123.4, correct
}
}
```

Tested with 4.0.0-rc.3+348192fff.
With Haxe 3.4, both traces are `str: 123.4`.

From the output of Haxe 4, I can see that it's trying to convert `"str: "` to `Decimal` and do a `+` operation with `d`:
```js
Test.main = function() {
var d = 123.4;
console.log("Test.hx:16:",_$Test_Decimal_$Impl_$.toString(_$Test_Decimal_$Impl_$.add(d,_$Test_Decimal_$Impl_$.fromString("str: "))));
console.log("Test.hx:17:","str: " + _$Test_Decimal_$Impl_$.toString(d));
};
```

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.