Browse Source

[C#] Add implicit conversion operators for cs.internal.Null<T>

Closes #4719.
Also corrected indentation levels in classCode\functionCode to match up with those of the generated code around them.
Vadim Dyachenko 9 years ago
parent
commit
7258a8bf61
1 changed files with 34 additions and 25 deletions
  1. 34 25
      std/cs/internal/Null.hx

+ 34 - 25
std/cs/internal/Null.hx

@@ -21,21 +21,33 @@
  */
 package cs.internal;
 
-@:classCode('
-	//This function is here to be used with Reflection, when the haxe.lang.Null type is known
-	public static haxe.lang.Null<T> _ofDynamic(object obj)
-	{
-		if (obj == null)
-		{
+@:classCode('//This function is here to be used with Reflection, when the haxe.lang.Null type is known
+		public static haxe.lang.Null<T> _ofDynamic(object obj) {
+			if (obj == null) {
+				return new haxe.lang.Null<T>(default(T), false);
+			} else if (typeof(T).Equals(typeof(double))) {
+				return new haxe.lang.Null<T>((T) (object) haxe.lang.Runtime.toDouble(obj), true);
+			} else if (typeof(T).Equals(typeof(int))) {
+				return new haxe.lang.Null<T>((T) (object) haxe.lang.Runtime.toInt(obj), true);
+			} else {
+				return new haxe.lang.Null<T>((T) obj, true);
+			}
+		}
+
+		public static implicit operator haxe.lang.Null<T>(T val) {
+			return new haxe.lang.Null<T>(val, true);
+		}
+
+		public static implicit operator T(haxe.lang.Null<T> obj) {
+			return obj.@value;
+		}
+
+		public static implicit operator Null<T>(__NoValue__ noValue) {
 			return new haxe.lang.Null<T>(default(T), false);
-		} else if (typeof(T).Equals(typeof(double))) {
-			return new haxe.lang.Null<T>((T) (object) haxe.lang.Runtime.toDouble(obj), true);
-		} else if (typeof(T).Equals(typeof(int))) {
-			return new haxe.lang.Null<T>((T) (object) haxe.lang.Runtime.toInt(obj), true);
-		} else {
-			return new haxe.lang.Null<T>((T) obj, true);
 		}
-	}
+
+		public class __NoValue__ { }
+
 ')
 #if core_api_serialize
 @:meta(System.Serializable)
@@ -56,18 +68,15 @@ package cs.internal;
 		untyped this.hasValue = hasValue;
 	}
 
-	@:functionCode('
-		if (obj == null)
-		{
-			return new haxe.lang.Null<D>(default(D), false);
-		} else if (typeof(D).Equals(typeof(double))) {
-			return new haxe.lang.Null<D>((D) (object) haxe.lang.Runtime.toDouble(obj), true);
-		} else if (typeof(D).Equals(typeof(int))) {
-			return new haxe.lang.Null<D>((D) (object) haxe.lang.Runtime.toInt(obj), true);
-		} else {
-			return new haxe.lang.Null<D>((D) obj, true);
-		}
-	')
+	@:functionCode('if (obj == null) {
+				return new haxe.lang.Null<D>(default(D), false);
+			} else if (typeof(D).Equals(typeof(double))) {
+				return new haxe.lang.Null<D>((D) (object) haxe.lang.Runtime.toDouble(obj), true);
+			} else if (typeof(D).Equals(typeof(int))) {
+				return new haxe.lang.Null<D>((D) (object) haxe.lang.Runtime.toInt(obj), true);
+			} else {
+				return new haxe.lang.Null<D>((D) obj, true);
+			}')
 	public static function ofDynamic<D>(obj:Dynamic):Nullable<D>
 	{
 		return null;