Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

to Float on abstract is getting ignored, can't do math operations #11576

Open
Jarrio opened this issue Feb 13, 2024 · 4 comments
Open

to Float on abstract is getting ignored, can't do math operations #11576

Jarrio opened this issue Feb 13, 2024 · 4 comments

Comments

@Jarrio
Copy link

Jarrio commented Feb 13, 2024

class Test {
  static function main() {
    var now = Date.now().getTime() - Foo.a;
    trace(now);
  }
}

enum abstract Foo(Float) to Float {
	var a = 10000001;
}

This results in the following error:

[ERROR] Test.hx:3: characters 38-43

 3 |     var now = Date.now().getTime() - Foo.a;
   |                                      ^^^^^
   | Foo should be Int

But everything here is typed and declared as Float so why is it that I can't do the subtraction?

@filt3rek
Copy link
Contributor

filt3rek commented Feb 14, 2024

Hej,

It works with that :

enum abstract Foo(Float) from Float to Float {
	var a : Float = 10000001;
}

But I agree it would be cool if it did the conversion by itself

@Jarrio
Copy link
Author

Jarrio commented Feb 14, 2024

Yes, there are ways to get around it but it changes what the intention of the code is, I don't want to allow any float for Foo.

The solution I do is type hitting Foo at declaration But I don't understand why when I specified that the type should be to Float

@kLabz
Copy link
Contributor

kLabz commented Feb 14, 2024

Current workaround is to add @:op there https://try.haxe.org/#B8D0053C or the short version https://try.haxe.org/#C2F8766b (see manual for this one)

@ibilon
Copy link
Member

ibilon commented Feb 14, 2024

the short version https://try.haxe.org/#C2F8766b

@:commutative can be used to avoid adding add2 and sub2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants