Ver código fonte

add haxe/Constraints.hx with ObjectMapKey (closes #1971)

Simon Krajewski 12 anos atrás
pai
commit
1054bd4c27

+ 2 - 2
std/Map.hx

@@ -79,7 +79,7 @@ abstract Map<K,V>(IMap<K,V> ) {
 		1. the map has no mapping for `key`
 		1. the map has no mapping for `key`
 		2. the map has a mapping with a value of `null`
 		2. the map has a mapping with a value of `null`
 		
 		
-		If it is important to distinguish these cases, `exists()` should be 
+		If it is important to distinguish these cases, `exists()` should be
 		used.
 		used.
 		
 		
 		If `key` is null, the result is unspecified.
 		If `key` is null, the result is unspecified.
@@ -145,7 +145,7 @@ abstract Map<K,V>(IMap<K,V> ) {
 		return new EnumValueMap<K, V>();
 		return new EnumValueMap<K, V>();
 	}
 	}
 
 
-	@:to static inline function toObjectMap<K:{ }>(t:IMap<K,V>):ObjectMap<K,V> {
+	@:to static inline function toObjectMap<K:haxe.Constraints.ObjectMapKey>(t:IMap<K,V>):ObjectMap<K,V> {
 		return new ObjectMap<K, V>();
 		return new ObjectMap<K, V>();
 	}
 	}
 	
 	

+ 1 - 1
std/cpp/_std/haxe/ds/ObjectMap.hx

@@ -22,7 +22,7 @@
 package haxe.ds;
 package haxe.ds;
 
 
 @:coreApi
 @:coreApi
-class ObjectMap<K:{},V> implements Map.IMap<K,V> {
+class ObjectMap<K:haxe.Constraints.ObjectMapKey,V> implements Map.IMap<K,V> {
 	private var __Internal : IntMap<V>;
 	private var __Internal : IntMap<V>;
 	private var __KeyRefs : IntMap<K>;
 	private var __KeyRefs : IntMap<K>;
 
 

+ 1 - 1
std/cs/_std/haxe/ds/ObjectMap.hx

@@ -23,7 +23,7 @@ package haxe.ds;
 
 
 import cs.NativeArray;
 import cs.NativeArray;
 
 
-@:coreApi class ObjectMap<K:{}, V> implements Map.IMap<K,V>
+@:coreApi class ObjectMap<K:haxe.Constraints.ObjectMapKey, V> implements Map.IMap<K,V>
 {
 {
 	@:extern private static inline var HASH_UPPER = 0.77;
 	@:extern private static inline var HASH_UPPER = 0.77;
 	@:extern private static inline var FLAG_EMPTY = 0;
 	@:extern private static inline var FLAG_EMPTY = 0;

+ 3 - 1
std/flash/_std/haxe/ds/ObjectMap.hx

@@ -1,7 +1,9 @@
 package haxe.ds;
 package haxe.ds;
 
 
+import haxe.Constraints;
+
 @:coreApi
 @:coreApi
-class ObjectMap<K:{},V> extends flash.utils.Dictionary implements Map.IMap<K,V> {
+class ObjectMap<K:ObjectMapKey,V> extends flash.utils.Dictionary implements Map.IMap<K,V> {
 
 
 	public function new() {
 	public function new() {
 		super(false);
 		super(false);

+ 5 - 3
std/flash8/_std/haxe/ds/ObjectMap.hx

@@ -22,16 +22,18 @@
 
 
 package haxe.ds;
 package haxe.ds;
 
 
+import haxe.Constraints;
+
 @:coreApi
 @:coreApi
-class ObjectMap <K:{ }, V> implements Map.IMap<K,V> {
+class ObjectMap <K:ObjectMapKey, V> implements Map.IMap<K,V> {
 	
 	
 	static var count = 0;
 	static var count = 0;
 	
 	
-	static inline function assignId(obj: { } ):Int {
+	static inline function assignId(obj:ObjectMapKey):Int {
 		return untyped obj.__id__ = ++count;
 		return untyped obj.__id__ = ++count;
 	}
 	}
 	
 	
-	static inline function getId(obj: { } ):Int {
+	static inline function getId(obj:ObjectMapKey):Int {
 		return untyped obj.__id__;
 		return untyped obj.__id__;
 	}
 	}
 	
 	

+ 37 - 0
std/haxe/Constraints.hx

@@ -0,0 +1,37 @@
+/*
+ * Copyright (C)2005-2013 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.
+ */
+
+package haxe;
+
+/**
+	This abstract is compatible to both its type parameters.
+	
+	If used as a type parameter constraint, the accepted types are `L` and `R`.
+	
+	If used as a real type, the underlying type will be `Dynamic`.
+**/
+abstract Or<L,R>(Dynamic) from L to L from R to R { }
+
+/**
+	The types allowed as key to `haxe.ds.ObjectMap`.
+**/
+extern typedef ObjectMapKey = Or<Class<Dynamic>, {}>;

+ 7 - 7
std/haxe/ds/ObjectMap.hx

@@ -30,7 +30,7 @@ package haxe.ds;
 	
 	
 	See `Map` for documentation details.
 	See `Map` for documentation details.
 **/
 **/
-extern class ObjectMap < K: { }, V > implements Map.IMap<K,V> {
+extern class ObjectMap<K:haxe.Constraints.ObjectMapKey, V> implements Map.IMap<K,V> {
 	
 	
 	/**
 	/**
 		Creates a new ObjectMap.
 		Creates a new ObjectMap.
@@ -44,31 +44,31 @@ extern class ObjectMap < K: { }, V > implements Map.IMap<K,V> {
 	
 	
 	/**
 	/**
 		See `Map.get`
 		See `Map.get`
-	**/	
+	**/
 	public function get(key:K):Null<V>;
 	public function get(key:K):Null<V>;
 	
 	
 	/**
 	/**
 		See `Map.exists`
 		See `Map.exists`
-	**/	
+	**/
 	public function exists(key:K):Bool;
 	public function exists(key:K):Bool;
 	
 	
 	/**
 	/**
 		See `Map.remove`
 		See `Map.remove`
-	**/	
+	**/
 	public function remove(key:K):Bool;
 	public function remove(key:K):Bool;
 	
 	
 	/**
 	/**
 		See `Map.keys`
 		See `Map.keys`
-	**/	
+	**/
 	public function keys():Iterator<K>;
 	public function keys():Iterator<K>;
 	
 	
 	/**
 	/**
 		See `Map.iterator`
 		See `Map.iterator`
-	**/	
+	**/
 	public function iterator():Iterator<V>;
 	public function iterator():Iterator<V>;
 	
 	
 	/**
 	/**
 		See `Map.toString`
 		See `Map.toString`
-	**/	
+	**/
 	public function toString():String;
 	public function toString():String;
 }
 }

+ 1 - 1
std/java/_std/haxe/ds/ObjectMap.hx

@@ -23,7 +23,7 @@ package haxe.ds;
 
 
 import java.NativeArray;
 import java.NativeArray;
 
 
-@:coreApi class ObjectMap<K:{}, V> implements Map.IMap<K,V>
+@:coreApi class ObjectMap<K:haxe.Constraints.ObjectMapKey, V> implements Map.IMap<K,V>
 {
 {
 	@:extern private static inline var HASH_UPPER = 0.77;
 	@:extern private static inline var HASH_UPPER = 0.77;
 	@:extern private static inline var FLAG_EMPTY = 0;
 	@:extern private static inline var FLAG_EMPTY = 0;

+ 5 - 3
std/js/_std/haxe/ds/ObjectMap.hx

@@ -22,16 +22,18 @@
 
 
 package haxe.ds;
 package haxe.ds;
 
 
+import haxe.Constraints;
+
 @:coreApi
 @:coreApi
-class ObjectMap<K:{ }, V> implements Map.IMap<K,V> {
+class ObjectMap<K:ObjectMapKey, V> implements Map.IMap<K,V> {
 	
 	
 	static var count = 0;
 	static var count = 0;
 	
 	
-	static inline function assignId(obj: { } ):Int {
+	static inline function assignId(obj:ObjectMapKey):Int {
 		return untyped obj.__id__ = ++count;
 		return untyped obj.__id__ = ++count;
 	}
 	}
 	
 	
-	static inline function getId(obj: { } ):Int {
+	static inline function getId(obj:ObjectMapKey):Int {
 		return untyped obj.__id__;
 		return untyped obj.__id__;
 	}
 	}
 	
 	

+ 5 - 3
std/neko/_std/haxe/ds/ObjectMap.hx

@@ -21,18 +21,20 @@
  */
  */
 package haxe.ds;
 package haxe.ds;
 
 
+import haxe.Constraints;
+
 @:coreApi
 @:coreApi
-class ObjectMap<K:{},V> implements Map.IMap<K,V> {
+class ObjectMap<K:ObjectMapKey,V> implements Map.IMap<K,V> {
 
 
 	static var count = 0;
 	static var count = 0;
 	
 	
-	static inline function assignId(obj: { } ):Int {
+	static inline function assignId(obj:ObjectMapKey):Int {
 		var newId = count++;
 		var newId = count++;
 		untyped obj.__id__ = newId;
 		untyped obj.__id__ = newId;
 		return newId;
 		return newId;
 	}
 	}
 	
 	
-	static inline function getId(obj: { } ):Int {
+	static inline function getId(obj:ObjectMapKey):Int {
 		return untyped obj.__id__;
 		return untyped obj.__id__;
 	}
 	}
 	
 	

+ 4 - 2
std/php/_std/haxe/ds/ObjectMap.hx

@@ -22,9 +22,11 @@
 
 
 package haxe.ds;
 package haxe.ds;
 
 
+import haxe.Constraints;
+
 @:coreApi
 @:coreApi
-class ObjectMap <K:{ }, V> implements Map.IMap<K,V> {
-	static function getId(key: { } ):String {
+class ObjectMap <K:ObjectMapKey, V> implements Map.IMap<K,V> {
+	static function getId(key:ObjectMapKey):String {
 		return untyped __php__("spl_object_hash($key)");
 		return untyped __php__("spl_object_hash($key)");
 	}
 	}