Explorar o código

add Any type (implements HXP-0001, closes #5322) (#5500)

Dan Korostelev %!s(int64=9) %!d(string=hai) anos
pai
achega
f72909dabe
Modificáronse 3 ficheiros con 72 adicións e 3 borrados
  1. 36 0
      std/Any.hx
  2. 4 3
      std/StdTypes.hx
  3. 32 0
      tests/unit/src/unit/issues/Issue5322.hx

+ 36 - 0
std/Any.hx

@@ -0,0 +1,36 @@
+/*
+ * Copyright (C)2005-2016 Haxe Foundation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+/**
+	`Any` is a type that is compatible with any other in both ways.
+
+	This means that a value of any type can be assigned to `Any`, and
+	vice-versa, a value of `Any` type can be assigned to any other type.
+
+	It's a more type-safe alternative to `Dynamic`, because it doesn't
+	support field access or operators and it's bound to monomorphs. So,
+	to work with the actual value, it needs to be explicitly promoted
+	to another type.
+**/
+abstract Any(Dynamic) from Dynamic {
+	@:noCompletion @:extern @:to inline function __promote<T>():T return this;
+}

+ 4 - 3
std/StdTypes.hx

@@ -34,7 +34,7 @@
 	On static targets, `null` cannot be assigned to Float. If this is necessary,
 	`Null<Float>` can be used instead.
 
-	`Std.int` converts a `Float` to an `Int`, rounded towards 0.  
+	`Std.int` converts a `Float` to an `Int`, rounded towards 0.
 	`Std.parseFloat` converts a `String` to a `Float`.
 
 	@see http://haxe.org/manual/types-basic-types.html
@@ -48,7 +48,7 @@
 	On static targets, `null` cannot be assigned to `Int`. If this is necessary,
 	`Null<Int>` can be used instead.
 
-	`Std.int` converts a `Float` to an `Int`, rounded towards 0.  
+	`Std.int` converts a `Float` to an `Int`, rounded towards 0.
 	`Std.parseInt` converts a `String` to an `Int`.
 
 	@see http://haxe.org/manual/types-basic-types.html
@@ -90,7 +90,8 @@ typedef Null<T> = T
 	`Dynamic` is a special type which is compatible with all other types.
 
 	Use of `Dynamic` should be minimized as it prevents several compiler
-	checks and optimizations.
+	checks and optimizations. See `Any` type for a safer alternative for
+	representing values of any type.
 
 	@see http://haxe.org/manual/types-dynamic.html
 **/

+ 32 - 0
tests/unit/src/unit/issues/Issue5322.hx

@@ -0,0 +1,32 @@
+package unit.issues;
+
+import unit.TestType.typeErrorText;
+import unit.TestType.typedAs;
+
+class Issue5322 extends unit.Test {
+	function test() {
+		// unifies!
+		var a:Any = 1;
+
+		// no fields!
+		eq("Any has no field f", typeErrorText(a.f));
+
+		// no array access!
+		eq("Array access is not allowed on Any", typeErrorText(a[0]));
+
+		// no comparison!
+		eq("Cannot compare Any and Int", typeErrorText(a > 1));
+
+		// kept as the return type!
+		typedAs(something(), (1:Any));
+
+		// promotes to another type successfully!
+		eq("HELLO", (something() : String).toUpperCase());
+
+		// can be used for array of mixed types!
+		var a:Array<Any> = [1,false,"hey",{}];
+		eq("hey", (a[2] : String));
+	}
+
+	function something():Any return "hello";
+}