|
@@ -25,7 +25,7 @@ class TestMatch extends Test {
|
|
} catch (e:Dynamic) Std.string(e.message);
|
|
} catch (e:Dynamic) Std.string(e.message);
|
|
return macro $v{result};
|
|
return macro $v{result};
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
static function switchNormal(e:Expr):String {
|
|
static function switchNormal(e:Expr):String {
|
|
return switch(e.expr) {
|
|
return switch(e.expr) {
|
|
case EConst(CString(s)): s;
|
|
case EConst(CString(s)): s;
|
|
@@ -42,7 +42,7 @@ class TestMatch extends Test {
|
|
"not_found";
|
|
"not_found";
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
static function switchCapture(e:Expr) {
|
|
static function switchCapture(e:Expr) {
|
|
return switch(e) {
|
|
return switch(e) {
|
|
case { expr : EConst(const = (CString("foobar") | CInt("9"))) } :
|
|
case { expr : EConst(const = (CString("foobar") | CInt("9"))) } :
|
|
@@ -51,7 +51,7 @@ class TestMatch extends Test {
|
|
null;
|
|
null;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
static function switchArray(e:Expr):String {
|
|
static function switchArray(e:Expr):String {
|
|
return switch(e.expr) {
|
|
return switch(e.expr) {
|
|
case EArrayDecl([]):
|
|
case EArrayDecl([]):
|
|
@@ -64,7 +64,7 @@ class TestMatch extends Test {
|
|
"_";
|
|
"_";
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
static function switchArray2(a:Array<String>):String {
|
|
static function switchArray2(a:Array<String>):String {
|
|
return switch(a) {
|
|
return switch(a) {
|
|
case ["a", "b"]: "0";
|
|
case ["a", "b"]: "0";
|
|
@@ -77,7 +77,7 @@ class TestMatch extends Test {
|
|
case _: "7";
|
|
case _: "7";
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
static function switchStructure(a: { foo:String, bar:String } ) {
|
|
static function switchStructure(a: { foo:String, bar:String } ) {
|
|
return switch(a) {
|
|
return switch(a) {
|
|
case { foo: "val1", bar:"val2" } : "0";
|
|
case { foo: "val1", bar:"val2" } : "0";
|
|
@@ -86,7 +86,7 @@ class TestMatch extends Test {
|
|
case { bar: a } : a;
|
|
case { bar: a } : a;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
static function switchCrazy(e:Expr) {
|
|
static function switchCrazy(e:Expr) {
|
|
return switch(e.expr) {
|
|
return switch(e.expr) {
|
|
case EUntyped( { expr : EParenthesis( { expr : EArray( { expr: a = EConst(CString(_)) }, { expr : EConst(CInt(b)) } ) } ) } ):
|
|
case EUntyped( { expr : EParenthesis( { expr : EArray( { expr: a = EConst(CString(_)) }, { expr : EConst(CInt(b)) } ) } ) } ):
|
|
@@ -95,7 +95,7 @@ class TestMatch extends Test {
|
|
"_";
|
|
"_";
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
static function switchGuard(e:Expr):String {
|
|
static function switchGuard(e:Expr):String {
|
|
return switch(e.expr) {
|
|
return switch(e.expr) {
|
|
case EConst(CString(s)) if (StringTools.startsWith(s, "foo")):
|
|
case EConst(CString(s)) if (StringTools.startsWith(s, "foo")):
|
|
@@ -110,15 +110,15 @@ class TestMatch extends Test {
|
|
"5";
|
|
"5";
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
static function switchClass<T>(cl:Class<T>) {
|
|
static function switchClass<T>(cl:Class<T>) {
|
|
return switch(cl) {
|
|
return switch(cl) {
|
|
case String: "String";
|
|
case String: "String";
|
|
- case haxe.Template: "haxe.Template";
|
|
|
|
|
|
+ case MyClass: "unit.MyClass";
|
|
case a: "other: " +Type.getClassName(a);
|
|
case a: "other: " +Type.getClassName(a);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
function testBasic() {
|
|
function testBasic() {
|
|
eq("bar", switchNormal(macro "bar"));
|
|
eq("bar", switchNormal(macro "bar"));
|
|
eq("bar", switchNormal(macro ("bar")));
|
|
eq("bar", switchNormal(macro ("bar")));
|
|
@@ -128,18 +128,18 @@ class TestMatch extends Test {
|
|
eq("22.5", switchNormal(macro null[22.5]));
|
|
eq("22.5", switchNormal(macro null[22.5]));
|
|
eq("EConst(CInt(0))", switchNormal(macro 1 in 0));
|
|
eq("EConst(CInt(0))", switchNormal(macro 1 in 0));
|
|
eq("not_found", switchNormal(macro null["22"]));
|
|
eq("not_found", switchNormal(macro null["22"]));
|
|
-
|
|
|
|
|
|
+
|
|
t(null != switchCapture(macro "foobar"));
|
|
t(null != switchCapture(macro "foobar"));
|
|
t(null == switchCapture(macro "fooba"));
|
|
t(null == switchCapture(macro "fooba"));
|
|
t(null != switchCapture(macro 9));
|
|
t(null != switchCapture(macro 9));
|
|
t(null == switchCapture(macro 10));
|
|
t(null == switchCapture(macro 10));
|
|
-
|
|
|
|
|
|
+
|
|
eq("[]", switchArray(macro []));
|
|
eq("[]", switchArray(macro []));
|
|
eq("_", switchArray(macro 2));
|
|
eq("_", switchArray(macro 2));
|
|
eq("[EConst(CInt(22))]", switchArray(macro [22]));
|
|
eq("[EConst(CInt(22))]", switchArray(macro [22]));
|
|
eq("[EConst(CInt(22)),EConst(CString(foo))]", switchArray(macro [22,"foo"]));
|
|
eq("[EConst(CInt(22)),EConst(CString(foo))]", switchArray(macro [22,"foo"]));
|
|
eq("_", switchArray(macro [22, "foo", "bar"]));
|
|
eq("_", switchArray(macro [22, "foo", "bar"]));
|
|
-
|
|
|
|
|
|
+
|
|
eq("0", switchArray2(["a", "b"]));
|
|
eq("0", switchArray2(["a", "b"]));
|
|
eq("1", switchArray2(["a"]));
|
|
eq("1", switchArray2(["a"]));
|
|
eq("2", switchArray2(["b"]));
|
|
eq("2", switchArray2(["b"]));
|
|
@@ -149,46 +149,46 @@ class TestMatch extends Test {
|
|
eq("5:3", switchArray2(["a","a","a"]));
|
|
eq("5:3", switchArray2(["a","a","a"]));
|
|
eq("6", switchArray2([]));
|
|
eq("6", switchArray2([]));
|
|
eq("7", switchArray2(["a", "a", "a", "b"]));
|
|
eq("7", switchArray2(["a", "a", "a", "b"]));
|
|
-
|
|
|
|
|
|
+
|
|
eq("EConst(CString(foobar)):12", switchCrazy(macro untyped ("foobar"[12])));
|
|
eq("EConst(CString(foobar)):12", switchCrazy(macro untyped ("foobar"[12])));
|
|
-
|
|
|
|
|
|
+
|
|
eq("1", switchGuard(macro "foobar"));
|
|
eq("1", switchGuard(macro "foobar"));
|
|
eq("2", switchGuard(macro "barfoo"));
|
|
eq("2", switchGuard(macro "barfoo"));
|
|
eq("3", switchGuard(macro 2));
|
|
eq("3", switchGuard(macro 2));
|
|
eq("4", switchGuard(macro 5));
|
|
eq("4", switchGuard(macro 5));
|
|
eq("4", switchGuard(macro "bazfoo"));
|
|
eq("4", switchGuard(macro "bazfoo"));
|
|
eq("5", switchGuard(macro []));
|
|
eq("5", switchGuard(macro []));
|
|
-
|
|
|
|
|
|
+
|
|
eq("0", switch ([true, 1, "foo"]) {
|
|
eq("0", switch ([true, 1, "foo"]) {
|
|
case [true, 1, "foo"]: "0";
|
|
case [true, 1, "foo"]: "0";
|
|
case [true, 1, _]: "1";
|
|
case [true, 1, _]: "1";
|
|
case _: "_";
|
|
case _: "_";
|
|
});
|
|
});
|
|
-
|
|
|
|
|
|
+
|
|
eq("0", switch [true, 1, "foo"] {
|
|
eq("0", switch [true, 1, "foo"] {
|
|
case [true, 1, "foo"]: "0";
|
|
case [true, 1, "foo"]: "0";
|
|
case [true, 1, _]: "1";
|
|
case [true, 1, _]: "1";
|
|
case _: "_";
|
|
case _: "_";
|
|
});
|
|
});
|
|
-
|
|
|
|
|
|
+
|
|
eq("1", switch [true, 1, "bar"] {
|
|
eq("1", switch [true, 1, "bar"] {
|
|
case [true, 1, "foo"]: "0";
|
|
case [true, 1, "foo"]: "0";
|
|
case [true, 1, _]: "1";
|
|
case [true, 1, _]: "1";
|
|
case _: "_";
|
|
case _: "_";
|
|
});
|
|
});
|
|
-
|
|
|
|
|
|
+
|
|
eq("_", switch [false, 1, "foo"] {
|
|
eq("_", switch [false, 1, "foo"] {
|
|
case [true, 1, "foo"]: "0";
|
|
case [true, 1, "foo"]: "0";
|
|
case [true, 1, _]: "1";
|
|
case [true, 1, _]: "1";
|
|
case _: "_";
|
|
case _: "_";
|
|
});
|
|
});
|
|
-
|
|
|
|
|
|
+
|
|
eq("1", switch [1, 2] {
|
|
eq("1", switch [1, 2] {
|
|
case [0, 0] | [1, 2]: "1";
|
|
case [0, 0] | [1, 2]: "1";
|
|
case [1, 1]: "2";
|
|
case [1, 1]: "2";
|
|
case _: "_";
|
|
case _: "_";
|
|
});
|
|
});
|
|
-
|
|
|
|
|
|
+
|
|
var t = TA("foo");
|
|
var t = TA("foo");
|
|
eq("0", switch(t) {
|
|
eq("0", switch(t) {
|
|
case TA("foo"): "0";
|
|
case TA("foo"): "0";
|
|
@@ -196,7 +196,7 @@ class TestMatch extends Test {
|
|
case TC(_): "2";
|
|
case TC(_): "2";
|
|
});
|
|
});
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
function testTuple() {
|
|
function testTuple() {
|
|
function test(a:Int, b:Int, c:Int) return switch [a, b, c] {
|
|
function test(a:Int, b:Int, c:Int) return switch [a, b, c] {
|
|
case [x, 1, 2] | [1, 2, x] | [1, x, 2]: '0|x:$x';
|
|
case [x, 1, 2] | [1, 2, x] | [1, x, 2]: '0|x:$x';
|
|
@@ -214,7 +214,7 @@ class TestMatch extends Test {
|
|
eq("2|y:9,z:8", test(2, 8, 9));
|
|
eq("2|y:9,z:8", test(2, 8, 9));
|
|
eq("_:x:9,y:8,z:7", test(9, 8, 7));
|
|
eq("_:x:9,y:8,z:7", test(9, 8, 7));
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
function testGrouping() {
|
|
function testGrouping() {
|
|
function test(v) return switch(v) {
|
|
function test(v) return switch(v) {
|
|
case 1, 2, 3: "0";
|
|
case 1, 2, 3: "0";
|
|
@@ -228,7 +228,7 @@ class TestMatch extends Test {
|
|
eq(results[i], test(i));
|
|
eq(results[i], test(i));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
function testSubtyping() {
|
|
function testSubtyping() {
|
|
var c = new MyClass.InitBase();
|
|
var c = new MyClass.InitBase();
|
|
var r = switch(c) {
|
|
var r = switch(c) {
|
|
@@ -238,13 +238,13 @@ class TestMatch extends Test {
|
|
"_";
|
|
"_";
|
|
}
|
|
}
|
|
eq("s = foo", r);
|
|
eq("s = foo", r);
|
|
-
|
|
|
|
|
|
+
|
|
eq("0", switchStructure( { foo:"val1", bar:"val2" } ));
|
|
eq("0", switchStructure( { foo:"val1", bar:"val2" } ));
|
|
eq("1", switchStructure( { foo:"val1", bar:"val1" } ));
|
|
eq("1", switchStructure( { foo:"val1", bar:"val1" } ));
|
|
eq("2", switchStructure( { foo:"val2", bar:"val2" } ));
|
|
eq("2", switchStructure( { foo:"val2", bar:"val2" } ));
|
|
eq("val1", switchStructure( { foo:"val2", bar:"val1" } ));
|
|
eq("val1", switchStructure( { foo:"val2", bar:"val1" } ));
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
public static function toStringX<Z>(x1:X<Z>) {
|
|
public static function toStringX<Z>(x1:X<Z>) {
|
|
return switch (x1) {
|
|
return switch (x1) {
|
|
case U1(x) if (x > 1): ">1";
|
|
case U1(x) if (x > 1): ">1";
|
|
@@ -253,19 +253,19 @@ class TestMatch extends Test {
|
|
case U2: "U2";
|
|
case U2: "U2";
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
function testGadt() {
|
|
function testGadt() {
|
|
eq("<=1", toStringX(U1(1)));
|
|
eq("<=1", toStringX(U1(1)));
|
|
eq(">1", toStringX(U1(2)));
|
|
eq(">1", toStringX(U1(2)));
|
|
eq("U2", toStringX(U2));
|
|
eq("U2", toStringX(U2));
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
function testClassSwitch() {
|
|
function testClassSwitch() {
|
|
eq("String", switchClass(String));
|
|
eq("String", switchClass(String));
|
|
- eq("haxe.Template", switchClass(haxe.Template));
|
|
|
|
|
|
+ eq("unit.MyClass", switchClass(MyClass));
|
|
eq("other: unit.TestMatch", switchClass(TestMatch));
|
|
eq("other: unit.TestMatch", switchClass(TestMatch));
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
function testNonExhaustiveness() {
|
|
function testNonExhaustiveness() {
|
|
eq("Unmatched patterns: false", getErrorMessage(switch(true) {
|
|
eq("Unmatched patterns: false", getErrorMessage(switch(true) {
|
|
case true:
|
|
case true:
|
|
@@ -291,7 +291,7 @@ class TestMatch extends Test {
|
|
case [_, true, _]:
|
|
case [_, true, _]:
|
|
}));
|
|
}));
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
function testInvalidBinding() {
|
|
function testInvalidBinding() {
|
|
eq("Variable y must appear exactly once in each sub-pattern", getErrorMessage(switch(Leaf("foo")) {
|
|
eq("Variable y must appear exactly once in each sub-pattern", getErrorMessage(switch(Leaf("foo")) {
|
|
case Leaf(x) | Leaf(y):
|
|
case Leaf(x) | Leaf(y):
|
|
@@ -312,7 +312,7 @@ class TestMatch extends Test {
|
|
case Node(l = Leaf(_), _) | Leaf(l):
|
|
case Node(l = Leaf(_), _) | Leaf(l):
|
|
}));
|
|
}));
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
#if false
|
|
#if false
|
|
//all lines marked as // unused should give an error
|
|
//all lines marked as // unused should give an error
|
|
function testRedundance() {
|
|
function testRedundance() {
|
|
@@ -321,32 +321,32 @@ class TestMatch extends Test {
|
|
case true:
|
|
case true:
|
|
case false: // unused
|
|
case false: // unused
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
switch(true) {
|
|
switch(true) {
|
|
case false | true:
|
|
case false | true:
|
|
case true: // unused
|
|
case true: // unused
|
|
case false: // unused
|
|
case false: // unused
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
switch(true) {
|
|
switch(true) {
|
|
case false
|
|
case false
|
|
| false: // unused
|
|
| false: // unused
|
|
case true:
|
|
case true:
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
switch(Leaf(true)) {
|
|
switch(Leaf(true)) {
|
|
case Leaf(true):
|
|
case Leaf(true):
|
|
case Leaf(false):
|
|
case Leaf(false):
|
|
case Leaf(x): // unused
|
|
case Leaf(x): // unused
|
|
case Node(_):
|
|
case Node(_):
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
switch({s:"foo"}) {
|
|
switch({s:"foo"}) {
|
|
case { s : "foo" } :
|
|
case { s : "foo" } :
|
|
case { s : a } : // Warning : This variable is unused
|
|
case { s : a } : // Warning : This variable is unused
|
|
case _: // unused
|
|
case _: // unused
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
switch( { s:"foo", t:"bar" } ) {
|
|
switch( { s:"foo", t:"bar" } ) {
|
|
case { s : "foo" }:
|
|
case { s : "foo" }:
|
|
case { t : "bar" }:
|
|
case { t : "bar" }:
|