Browse Source

Merge pull request #3465 from nadako/eithertype

add haxe.EitherType (see HaxeFoundation/hxnodejs#15)
Dan Korostelev 11 years ago
parent
commit
469540726c
3 changed files with 21 additions and 0 deletions
  1. 1 0
      extra/CHANGES.txt
  2. 12 0
      std/haxe/EitherType.hx
  3. 8 0
      tests/unit/unitstd/haxe/EitherType.unit.hx

+ 1 - 0
extra/CHANGES.txt

@@ -50,6 +50,7 @@
 
 	all : added haxe.ds.Either
 	all : added haxe.Rest type for representing "rest" arguments in extern method signatures
+	all : added haxe.EitherType abstract type for dealing with externs for dynamic targets
 
 	Macro features and changes:
 

+ 12 - 0
std/haxe/EitherType.hx

@@ -0,0 +1,12 @@
+package haxe;
+
+/**
+    An abstract type allowing values to be either of `T1` or `T2` type.
+    Supports implicit casts from/to either types.
+
+    It is useful for interfacing with external code on dynamic platforms
+    such as JavaScript or Python.
+
+    Otherwise, use of this type is discouraged.
+**/
+abstract EitherType<T1,T2>(Dynamic) from T1 to T1 from T2 to T2 {}

+ 8 - 0
tests/unit/unitstd/haxe/EitherType.unit.hx

@@ -0,0 +1,8 @@
+var e:haxe.EitherType<Int,String> = "string";
+var s:String = e;
+s == "string";
+e = 1;
+var i:Int = e;
+i == 1;
+TestType.typeError(e = false) == true;
+TestType.typeError(e = 1.5) == true;