Sfoglia il codice sorgente

define == and != between UInt and Int (see #2871)

Simon Krajewski 11 anni fa
parent
commit
0684826446
2 ha cambiato i file con 24 aggiunte e 3 eliminazioni
  1. 10 3
      std/UInt.hx
  2. 14 0
      tests/unit/issues/Issue2871.hx

+ 10 - 3
std/UInt.hx

@@ -129,11 +129,19 @@ abstract UInt(Int) from Int to Int {
 		return a.toFloat() > b;
 	}
 
-	@:commutative @:op(A == B) private static inline function equalsFloat(a:UInt, b:Float):Bool {
+	@:commutative @:op(A == B) private static inline function equalsInt<T:Int>(a:UInt, b:T):Bool {
+		return a.toInt() == b;
+	}
+
+	@:commutative @:op(A != B) private static inline function notEqualsInt<T:Int>(a:UInt, b:T):Bool {
+		return a.toInt() != b;
+	}
+
+	@:commutative @:op(A == B) private static inline function equalsFloat<T:Float>(a:UInt, b:T):Bool {
         return a.toFloat() == b;
     }
 
-    @:commutative @:op(A != B) private static inline function notEqualsFloat(a:UInt, b:Float):Bool {
+    @:commutative @:op(A != B) private static inline function notEqualsFloat<T:Float>(a:UInt, b:T):Bool {
         return a.toFloat() != b;
     }
 
@@ -158,7 +166,6 @@ abstract UInt(Int) from Int to Int {
 		return a.toFloat() <= b;
 	}
 
-
 	@:op(A < B) private static inline function floatLt(a:Float, b:UInt):Bool {
 		return a < b.toFloat();
 	}

+ 14 - 0
tests/unit/issues/Issue2871.hx

@@ -0,0 +1,14 @@
+package unit.issues;
+
+class Issue2871 extends Test {
+	#if !java
+    function call(myUInt:Null<UInt> = null) {
+        return myUInt == null ? 0 : myUInt;
+    }
+
+	function test() {
+		eq(0, call(null));
+		eq(1, call((1:UInt)));
+	}
+	#end
+}