Browse Source

add Function and FlatEnum constraint (closes #1826)

Simon Krajewski 12 years ago
parent
commit
989ca949d0
2 changed files with 24 additions and 4 deletions
  1. 20 4
      std/haxe/Constraints.hx
  2. 4 0
      type.ml

+ 20 - 4
std/haxe/Constraints.hx

@@ -23,15 +23,31 @@
 package haxe;
 package haxe;
 
 
 /**
 /**
-	This abstract is compatible to both its type parameters.
+	This type unifies with any function type.
 	
 	
-	If used as a type parameter constraint, the accepted types are `L` and `R`.
+	It is intended to be used as a type parameter constraint. If used as a real
+	type, the underlying type will be `Dynamic`.
+**/
+abstract Function(Dynamic) { }
+
+/**
+	This type unifies with an enum instance if all constructors of the enum
+	require no arguments.
+	
+	It is intended to be used as a type parameter constraint. If used as a real
+	type, the underlying type will be `Dynamic`.
+**/
+abstract FlatEnum(Dynamic) { }
+
+/**
+	This type is compatible with both its type parameters.
 	
 	
-	If used as a real type, the underlying type will be `Dynamic`.
+	It is intended to be used as a type parameter constraint. 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 { }
 abstract Or<L,R>(Dynamic) from L to L from R to R { }
 
 
 /**
 /**
 	The types allowed as key to `haxe.ds.ObjectMap`.
 	The types allowed as key to `haxe.ds.ObjectMap`.
 **/
 **/
-extern typedef ObjectMapKey = Or<Class<Dynamic>, {}>;
+extern typedef ObjectMapKey = Or<Class<Dynamic>, {}>;

+ 4 - 0
type.ml

@@ -1155,6 +1155,10 @@ let rec unify a b =
 		| _ -> error [cannot_unify a b])
 		| _ -> error [cannot_unify a b])
 	| TEnum _, TAbstract ({ a_path = [],"EnumValue" },[]) ->
 	| TEnum _, TAbstract ({ a_path = [],"EnumValue" },[]) ->
 		()
 		()
+	| TEnum(en,_), TAbstract ({ a_path = ["haxe"],"FlatEnum" },[]) ->
+		PMap.iter (fun _ ef -> match follow ef.ef_type with TFun _ -> error [cannot_unify a b] | _ -> ()) en.e_constrs	
+	| TFun _, TAbstract ({ a_path = ["haxe"],"Function" },[]) ->
+		()		
 	| TDynamic t , _ ->
 	| TDynamic t , _ ->
 		if t == a then
 		if t == a then
 			()
 			()