瀏覽代碼

added Class<T>

Nicolas Cannasse 18 年之前
父節點
當前提交
de55d4774a
共有 6 個文件被更改,包括 49 次插入23 次删除
  1. 1 0
      doc/CHANGES.txt
  2. 31 0
      std/Class.hx
  3. 2 2
      std/Reflect.hx
  4. 12 19
      std/Type.hx
  5. 1 2
      std/haxe/Unserializer.hx
  6. 2 0
      type.ml

+ 1 - 0
doc/CHANGES.txt

@@ -12,6 +12,7 @@
 	fixed Flash9 bugs in Date object
 	fixed dynamic Array typing bug
 	allowed typedef private field access
+	added Class<T> base class type
 
 2006-11-22: 1.09
 	added neko.vm.Module and neko.vm.Loader

+ 31 - 0
std/Class.hx

@@ -0,0 +1,31 @@
+/*
+ * Copyright (c) 2005, The haXe Project Contributors
+ * All rights reserved.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   - Redistributions of source code must retain the above copyright
+ *     notice, this list of conditions and the following disclaimer.
+ *   - Redistributions in binary form must reproduce the above copyright
+ *     notice, this list of conditions and the following disclaimer in the
+ *     documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ */
+
+/**
+	An abstract type that represents a Class.
+	See [Type] for the haXe Reflection API.
+**/
+extern class Class<T> {
+}

+ 2 - 2
std/Reflect.hx

@@ -50,12 +50,12 @@ class Reflect {
 	/**
 		Creates an instance of the given class with the list of constructor arguments.
 	**/
-	public static function createInstance( cl : Dynamic, args : Array<Dynamic> ) : Dynamic {
+	public static function createInstance<T>( cl : Class<T>, args : Array<Dynamic> ) : T untyped {
 		#if flash9
 			return cl.__construct__.call(null,args);
 		#else flash
 			var o = { __constructor__ : cl, __proto__ : cl.prototype };
-			cl[untyped "apply"](o,args);
+			cl["apply"](o,args);
 			return o;
 		#else neko
 			return untyped __dollar__call(__dollar__objget(cl,__dollar__hash("new".__s)),cl,args.__a);

+ 12 - 19
std/Type.hx

@@ -1,11 +1,4 @@
 
-/**
-	An abstract type that represents a Class.
-	See [Type] for the haXe Reflection API.
-**/
-enum Class {
-}
-
 /**
 	An abstract type that represents an Enum.
 	See [Type] for the haXe Reflection API.
@@ -24,7 +17,7 @@ enum ValueType {
 	TBool;
 	TObject;
 	TFunction;
-	TClass( c : Class );
+	TClass( c : Class<Dynamic> );
 	TEnum( e : Enum );
 	TUnknown;
 }
@@ -52,11 +45,11 @@ class Type {
 			return null;
 		}
 	}
-	
+
 	/**
 		Converts a value to a Class or returns [null] if the value is not a Class.
 	**/
-	public static function toClass( t : Dynamic ) : Class untyped {
+	public static function toClass( t : Dynamic ) : Class<Dynamic> untyped {
 		try {
 		#if flash9
 			if( !t.hasOwnProperty("prototype") )
@@ -70,11 +63,11 @@ class Type {
 			return null;
 		}
 	}
-	
+
 	/**
 		Returns the class of a value or [null] if this value is not a Class instance.
 	**/
-	public static function getClass( o : Dynamic ) : Class untyped {
+	public static function getClass<T>( o : T ) : Class<T> untyped {
 		#if flash9
 			var cname = __global__["flash.utils.getQualifiedClassName"](o);
 			if( cname == "null" || cname == "Object" || cname == "int" || cname == "Number" || cname == "Boolean" )
@@ -139,7 +132,7 @@ class Type {
 	/**
 		Returns the super-class of a class, or null if no super class.
 	**/
-	public static function getSuperClass( c : Class ) : Class untyped {
+	public static function getSuperClass<T>( c : Class<T> ) : Class<Dynamic> untyped {
 		#if flash9
 			var cname = __global__["flash.utils.getQualifiedSuperclassName"](c);
 			if( cname == "Object" )
@@ -154,7 +147,7 @@ class Type {
 	/**
 		Returns the complete name of a class.
 	**/
-	public static function getClassName( c : Class ) : String {
+	public static function getClassName( c : Class<Dynamic> ) : String {
 		if( c == null )
 			return null;
 		#if flash9
@@ -185,8 +178,8 @@ class Type {
 		Evaluates a class from a name. The class must have been compiled
 		to be accessible.
 	**/
-	public static function resolveClass( name : String ) : Class {
-		var cl : Class;
+	public static function resolveClass( name : String ) : Class<Dynamic> {
+		var cl : Class<Dynamic>;
 		untyped {
 		#if flash9
 			try {
@@ -269,7 +262,7 @@ class Type {
 		Similar to [Reflect.createInstance] excepts that the constructor is not called.
 		This enables you to create an instance without any side-effect.
 	**/
-	public static function createEmptyInstance( cl : Class ) untyped {
+	public static function createEmptyInstance<T>( cl : Class<T> ) : T untyped {
 		#if flash9
 			return cl.__construct__.call(null,null);
 		#else flash
@@ -305,7 +298,7 @@ class Type {
 	/**
 		Returns the list of instance fields.
 	**/
-	public static function getInstanceFields( c : Class ) : Array<String> {
+	public static function getInstanceFields( c : Class<Dynamic> ) : Array<String> {
 		#if flash9
 			return describe(c,true);
 		#else true
@@ -328,7 +321,7 @@ class Type {
 	/**
 		Returns the list of a class static fields.
 	**/
-	public static function getClassFields( c : Class ) : Array<String> {
+	public static function getClassFields( c : Class<Dynamic> ) : Array<String> {
 		#if flash9
 			var a = describe(c,false);
 			a.remove("__construct__");

+ 1 - 2
std/haxe/Unserializer.hx

@@ -23,11 +23,10 @@
  * DAMAGE.
  */
 package haxe;
-import Type.Class;
 import Type.Enum;
 
 typedef TypeResolver = {
-	function resolveClass( name : String ) : Class;
+	function resolveClass( name : String ) : Class<Dynamic>;
 	function resolveEnum( name : String ) : Enum;
 }
 

+ 2 - 0
type.ml

@@ -548,6 +548,8 @@ let rec unify a b =
 		(match !t with
 		| None -> if not (link t b a) then error [cannot_unify a b]
 		| Some t -> unify a t)
+	| TType ({ t_static = Some cl },params), TInst ({ cl_path = [],"Class" },[_,pt]) ->
+		unify (TInst (cl,params)) pt
 	| TType (t,tl) , _ ->
 		(try
 			unify (apply_params t.t_types tl t.t_type) b