HaxeFoundation / HaxeFoundation/haxe

Abstract types declared as inline return a compiler error with -debug mode on

Open
#9,136 3 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Haxe
Stars
6.9k
Forks
715
Avg merge
2d 2h
Merged PRs (30d)
11

Description

Hi there,

I've created an abstract Float type to help with implicit casts between Float and Int (akin to how AS3 does it):
```haxe
package;

abstract AbsFloat(Float) from Float to Float {
public inline function new(i:Float) {
this = i;
}

@:to
public inline function toInt() {
return Std.int(this);
}

@:to
public inline function toFloat() {
return this;
}

@:from
static public inline function fromFloat(f:Float) {
return new AbsFloat(f);
}

@:from
static public inline function fromInt(i:Int) {
return new AbsFloat(i);
}
}

class Test
{
public static inline var k : AbsFloat = 1;

static function main()
{
var i2:Int = k;
trace("Haxe is great! " + i2);
}
}
```

I'm creating a field `public static inline var k : AbsFloat = 1;`, notably initializing it to an `Int` value rather than a `Float`. The Haxe compiler should be able to handle that case afaik.

When I compile this code _without -debug_, I get no errors:
`haxe -js Main Main.hx`

Yay.
However, when I add `-debug`, I get the following error:
```
haxe -js Main Main.hx -debug
Main.hx:31: characters 2-44 : Inline variable initialization must be a constant value
```

I get the same behaviour with Haxe 3.4.7 and 4.0.5.

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.