HaxeFoundation / haxe-evolution

Repository for maintaining proposal for changes to the Haxe programming language
111 stars 58 forks source link

Integer data type #101

Open flashultra opened 1 year ago

flashultra commented 1 year ago

Update The following changes are now suggested for this proposal:

Rendered version

Apprentice-Alchemist commented 1 year ago

but real support for Int64 ( replacing abstract class with the native for the target) will give better performace and will lead to more clean code

That already happen on supported targets (C++, HL, C#, etc) Using eg Float on JS might be possible, but we need to be careful to keep behavior consistent between targets.

I'm also not a fan of the compiler implicitly deciding of the integer type. Numbers without suffixes (and with no type hints) should default to Int and give an error for too large literals. So var x:Int64 = 0xFFFFFFFFFF; should be allowed, but not var x = 0xFFFFFFFFFF;.

Other things could be adding universal UIn64 and unification of Int and Int32 types.

This would be nice, but again, we need to be careful about target-specific behavior.

flashultra commented 1 year ago

In practical is very rarely to use Int above 2147483647, so to not loose performance the current behaviour could be kept i.e. default Int (according to the target) and if someone want consistent overflow behaviour to use Int32. I did some tests on that ( for Firefox: https://www.measurethat.net/Benchmarks/Show/22863/0/int-32-bit-test#latest_results_block ) For Chrome results are better. So, to not lose performance the current solution (Int and Int32) is maybe good.

Int64 for Javascript could be a tricky one. Using Bigint for replacement could lead to loose of performance. I did some test for : Int64 (Haxe) , BigInt (JS) and Float ( Haxe) . The float give wrong result. BigInt give better performance than the current Int64 abstract class. Here is the example: https://try.haxe.org/#669Ae823

Int64 BigInt Float
Time 0.04929 0.00570 0.00190
Result Correct Correct Incorrect

Numbers without suffixes (and with no type hints) should default to Int

Default Int looks as a good solution . The other one could be to to use only signed integer values i.e Int < Int64 < BigInt depending of the value.

Adding UIn64 will be nice addition. Some target already have a native one ( C/C++ - unsigned long long ; C# - ulong; ) for other could be emulated or used BigInteger.