Redesign Int32 operator overloads with scalable type-parameter bounds; add compare()#12837
Redesign Int32 operator overloads with scalable type-parameter bounds; add compare()#12837Simn merged 3 commits intohaxe-numericfrom
Int32 operator overloads with scalable type-parameter bounds; add compare()#12837Conversation
…; add compare() Co-authored-by: Simn <634365+Simn@users.noreply.github.com>
@:op methods in Int32 with explicit implementations; add compare()
|
@copilot I'm looking at this thinking that it can't be the right approach to exhaustively list all allowed type permutations for all operators... this also isn't going to scale well if we add more integer types like Int16 and Int8 which are also assignable to Int, and thus can be compared to Int32. Could you check if there's a better design to handle this? I'm sure others have come up with a cleaner approach. |
…omotions; remove exhaustive mixed-type permutations Co-authored-by: Simn <634365+Simn@users.noreply.github.com>
The cleaner design is in commit 617efe6. The key mechanisms:
Result: ~50 overloads → 35 overloads. The pattern is now identical to |
@:op methods in Int32 with explicit implementations; add compare()Int32 operator overloads with scalable type-parameter bounds; add compare()
|
I'm concerned that this |
Int32previously had ~40@:opmethods — many body-less (delegating to the underlyingInt), and many exhaustively listing every type permutation (Int32×Int, Int×Int32, Int32×Float, Float×Int32, etc.). This approach does not scale: adding future types likeInt8/Int16would require updating every integer abstract with new overloads.Changes
haxe/Int32.hx: Redesigned operator overloads using the scalable pattern already present in non-FlashUInt.hx:+,-,*,%): onlyInt32×Int32; thefrom Intimplicit conversion auto-promotesIntin both operand positions, so no separateaddInt/intSub/etc. overloads are needed@:to toFloat()auto-degradesInt32toFloatforInt32+FloatandFloat+Int32cases==,!=):@:commutative equalsInt<T:Int>+@:commutative equalsFloat<T:Float>— two overloads cover all integer and float types, and will automatically cover futureInt8/Int16types withto Int<,<=,>,>=): onlyInt32×Int32viacompare();from Inthandles mixed cases in both positionsltFloat<T:Float>+floatLt<T:Float>pairs per operator (non-commutative, but<T:Float>bounds meansFloat32etc. will work automatically)&,|,^): onlyInt32×Int32;from Inthandles mixedb:Int; Int32 shift amounts degrade viato Inthaxe/numeric/Int32Native.hx/Int32Direct.hx: Addedcompare()(safe signed comparison via</>, not subtraction which overflows at boundaries) andmod().Result
~50 overloads → ~35 overloads. Future integer types (
Int8,Int16) withto Intwill interoperate withInt32operators automatically, with no changes needed toInt32.hx.Operator design summary
+,-,*,%)Int32×Int32;from Intauto-promotes/)Int32×Int32 → Floatexplicit==,!=)@:commutative <T:Int>+@:commutative <T:Float>Int32×Int32viacompare();from Inthandles mixedltFloat<T:Float>+floatLt<T:Float>&,|,^)Int32×Int32;from Intauto-promotesInt32×Int; Int32 shift amount degrades viato Int🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.